Programming Languages

by Nick Wong ‘20


What is coding?

  • Just taking ideas that we use in every day life, and writing them down into special forms known as programming languages, each of which has its own syntax (like a grammar), vocabulary, and conventions (like the special phrases you use in your own language)
  • In many ways, coding is just like writing in a normal human language; there are special rules and vocabulary

Programming Constructs

  • functions
  • loops
  • variables
  • conditions
  • We use the above to tell a computer what to do, similar to how you might need to direct a toddler to do something - very specifically
  • For example, the command move might be some form of function, which allows us to tell the computer to do something relatively complex (lift up right leg, push right leg forward, put right leg down, shift weight forward, lift left leg, etc) in one command - move
  • If we wanted to do something many times, it is more effective for us to use some form of loop to repeatedly perform and action - i.e. move while you are still standing, for 10 steps move
  • Sometimes, loops can go on forever, which can be bad or intentional, but in the first case, we woud call this a bug, or a mistake in the code
  • Many loops use something called a condition to continue looping - we do this intuitively. For example, in the move example above, I could tell you to move until you’ve hit a wall, or an edge, or a table. Maybe I tell you while you_have_not_hit_table, move, which looks much more like code, but still makes sense to us
  • variables, like x and y, but can be named more helpful things, like counter, isAlive, current_date, and so on
  • variables help us keep track of things in code

Scratch

  • Not all computer languages are based on text - for example, Scratch (thanks MIT!)
  • “Hello, world!” is the one of the most common introductions to computer science, almost everyone gets started in any new language by running some version of “Hello, world!”
  • Sprites are entities or characters that are displayed on a computer screen by some program, often a game, that allows the user to interact with the computer visually
  • Computers are not particularly smart on their own - they will only do exactly what you tell them to do
  • If we put only “When Green Flag Clicked”, the computer won’t do anything, even if we click the green flag
  • If we want to have the computer do anything else, we’ll need to include that after our “When Green Flag Clicked”

Online Resources

  • If you feel somewhat lost when you’re learning a new language, don’t worry! Everyone does! We all need to use resources to help ourselves learn new things
  • If you were learning a new spoken language, you’d likely seek some form of teacher - a tutor, a class, a friend - in order to learn it, and similarly, we have Google, Bing, Yahoo, Stack Overflow, and other great online resources to teach us how to use a given computer language

Conditions

  • Maybe we don’t want Scratch to walk into a wall and get stuck there every time we run our program
  • boolean expressions are statements that only have 2 possible answers - True/False, Yes/No, 1/0
  • We can use these booleans to check for things affecting our code
  • If these sound scary, don’t worry - you use them all the time in everyday language: “If you have the time…”, “While you’re at the store…”, etc.
  • In the above two cases, “have_time” and “atStore” might be two boolean expressions where their only two values are True and False - either you have time (True) or you don’t (False); either you’re at the store (True) or you’re not (False)

Pseudorandomness

  • Computers aren’t very good at being random - they do exactly what you tell them to
  • However, you can get close to being random by using some mathematical tricks, which works for most games and other general uses for randomness
  • Randomness in computers is hugely important to the topic of cryptography which is a major field in the modern world

Final Thoughts on Scratching the Surface

  • Even in something as “simple” looking as Scratch, it will make your life easier to build code in small chunks, where spotting bugs is much easier than if you were to search for a bug, or mistake in a huge web of code

C

  • C, our introduction to a “real-world” programming language (though Scratch also has significant power as a way of programming)
  • C looks more like what we think of when we think of computer science

Compilation

  • We understand source code, which is what we write, with words and semicolons, and other human-readable symbols
  • Computers read 0’s and 1’s though, and we need some method for getting here from our source code
  • The translator between these two languages (source code and machine code) is called a compiler
  • This translator, or compiler is another program on our computer that compiles source code down into machine code, which our computer can then read and do

Python

  • Instead of writing in C, which requires that we remember a lot about specifically how we code, we could write in something like Python, which is a “higher level language”, or allows us to forego a lot of the syntactical specificity of C (no semicolons, brackets, etc.)
  • Python actually doesn’t get compiled straight to machine code - instead, it needs to be interpreted into byte code
  • This byte code is merely another form for the code, which is readable by something like a virtual machine
  • Virtual machines can be thought of as imaginary computers within our own computer that allow us to run code in special cases or environments

Hello.c

  • We can write code in a text editor - Notepad, Visual Studio Code, vim, nano, even Word (though not recommended)
  • For example: ```c #include

int main(void) { printf(“Hello, world!”); }

* The above program allows us to print "Hello, world!" to something called a *console*, or *terminal*
* Both of these refer to the interface that we see when we're typing only commands, without the luxury of a mouse or other *Graphical Interface*, which merely means visual method for interacting with the user
* We can compile the above code using a special command (in our terminal or console) as follows:
```bash
clang -o hello hello.c
  • This compiles hello.c into a new file “hello”
  • Then,
    ./hello
    

    Tells our computer to run the program we just created


Hello.py

  • Instead of doing the above work to print “Hello, world!” we could use Python:
    print("Hello, world!")
    
  • And now running
    python hello.py
    

    will give us the same as hello.c, but with arguably much less effort


Other Languages

  • Ruby (on Rails), LISP, Java, C++, C#, JavaScript, Haskell, R, Swift
  • A lot of languages exist out there, and many of them have varying reasons they are or aren’t used in different contexts

Closing Thoughts

  • Many languages exist for interacting with the World Wide Web
  • To get there, we will need to talk about how do languages like HTML work, how websites work, how communication across these things work, and generally, how the Internet works