< PrevAllNext >

Computer Science 101: Programming Languages

(Dec 11th, 2013 at 01:51:20 AM)

In this next installment, it probably makes sense to start giving a few examples of what "code" looks like, and how algorithms are used by programmers. But first, it's worth describing what a programming language is.

Programming Languages

As we'll see tomorrow, making computers perform even simple calculations can be get complicated fast. That's because when you're building the circuitry for a computer, you have to piece together every function you want the computer to handle on its most basic level. If you make it too simple (for example only making it add and subtract numbers), you might still be able to do something like multiplication, it'll just be a lot harder. On the other hand, building multiplication into the circuits may take you a while to figure out, while piecing it together with addition might not be so bad.

Regardless, at some point you'll have figured out whatever balance works for you and be ready to start making programs. The key thing here is that it would be impossible to build everything you'll ever want into the hardware (or at least insanely complicated). You see, conceptually, computer programs are basically just sets of commands for the computer. Add the first number and the second number. Add the third number to that result. Display the final result. And to the programmer, that's exactly what's happening.

To get the computer to understand this, programmers use programs called compilers to translate their preferred method of describing programs into binary (or other code, as we'll find out later). This "method of describing programs" is the programming language. There are hundred of languages out there, from Java to Python to C++ to Fortran. Each has their own quirks, and for many there are several compilers that also have differences. Just as there are many people and many programs, there are many opinions about how to write code.

Hello, world!

As any programmer would tell you, the first step to learning any programming languages is to say "Hello, world!" This is largely for historical reasons, but it's also something simple that can give you an idea of what a language looks like and how it works. For example:

print "Hello, world!"
This is the hello world program in Python. Python is a very straightforward language designed to be simple but expressive enough to be useful. Good Python code is almost completely human readable, which is a programmer's way of saying that it looks like sentences. To contrast, here is the hello world program in C++ (my personal favorite language for most things):
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, world!" << endl;

This is obviously much more complicated. For something as basic as displaying some text, we already have a lot more code. There are a few things that play into this. Primarily just that C++ was invented in 1983, while Python was invented by 1991. Though ultimately not that long, it's an eternity in technology world. There are many complicated reasons why I prefer C++ to Python in general, but ultimately it just depends on what I'm writing and why. For smaller, less serious programs I'll probably use Python. It's easy and I won't waste any time with semantics. For larger programs that need to be fast, I'll use C++. These kinds of trade-off exist across nearly all programming languages, and of course each programmer has their own unique set of opinions.

Another Example

Finally, as a slightly more non-trivial example (non-trivial is a very common term in the computer world, basically just meaning less simple or easy), here are Python and C++ programs to create a list of five numbers (3, 2, 4, 5, 1) and print them out on separate lines in ascending order.

numbers = [3, 2, 4, 5, 1]

for number in sorted(numbers):
    print number

Here I am creating the list (numbers) and then for each "number" in the sorted list (sorted(numbers)), I am printing that number (print i).

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
        vector<int> numbers = {3, 2, 4, 5, 1};

        sort(numbers.begin(), numbers.end());

        for (int i=0; i<numbers.size(); i++) {
                cout << numbers[i] << endl;

Here I am making sure that I have what I near to print, store, and sort the list (the #include lines), then within "int main()" which is required in all C++ programs, I am creating "numbers" with the appropriate values, sorting them, and then creating something called "i" which starts at zero and increases until it is equal to the size of the list, printing out the number on it own line each time (again, there is a lot more going on). However, my even for something this simple the performance difference is noticeable. On my computer, the Python program takes about 3 milliseconds to run on average. The C++ program takes less than 1 millisecond. For a more complicated program there could be a serious difference.

An important thing to note in both of these programs is that I didn't write an algorithm to sort the list in either case (which is the hardest part of all of this). I was able to just say "sorted" in Python and "sort" in C++ to make that happen. Things like this are referred to as functions and are basically small parts of programs that meant to be re-used. That way every time a programmer wants to sort of list, they just call the appropriate function and it works. The more programming you do, the more you want to use pre-existing functions to do your work for you. A large part of being an effective programmer is writing functions that can help you as you continue to work on a given project.

But hopefully this gives you at least a little idea of what programming languages can look like and some of the interesting decisions and concepts that go into computer science as a whole. Next time, we'll look at how computers work.