Computer Programming for Absolute Beginners
上QQ阅读APP看书,第一时间看更新

Why do we have programming languages?

Machine code is very difficult. As we saw in the previous chapter, machine code is not made for us humans. It is perfect for computers, but we need something more comfortable to read, write, and understand.

The time it takes to write a program,  find errors and bugs in code, and update a program to add new features costs money. If the language we use can help us reduce the chance of introducing errors in code, it will reduce the costs. If it helps us understand the code when we read it, it will let us add new features faster, and so reduce costs. One goal of a programming language is that it must help us be efficient when we write programs.

It is at this point that the higher-level programming languages enter the scene. They enable us to write our code in something that often, at least to some degree, resembles English. In Chapter 1, Introduction to Computer Programs we saw one attempt to do this: assembly language. The introduction to this language helped somewhat, but it was still not good enough. What we need is something closer to human language.

Look at the following code snippet:

.data

    msgEqual db "Equal","$"

    msgNotEqual  db "Not Equal","$"

.code

main proc

    mov bl,"Alice"                  

    mov bh,"Bob"                  

    cmp bh,bl                   

    jne NotEqual                

    mov ax, seg msgEqual        

    mov ds, ax                 

    mov ah, 09h                 

    lea dx, msgEqual            

    int 21h                     

    mov ah, 4Ch                 

    int 21h                     

NotEqual:

    mov ax, seg msgNotEqual

    mov ds, ax

    mov ah, 09h

    lea dx, msgNotEqual

    int 21h

    

    mov ah, 4Ch   

    int 21h   

main endp

end main

Now, compare it to the following code:

IF "Alice" == "Bob" THEN

    print "Equal"

ELSE

   print "Not Equal"

ENDIF

Believe it or not, they both do the same thing. The first one is in assembly language and the second one is something that resembles a high-level language. Even if you have never seen code before, it is not hard to understand what this program is doing. It compares two text strings, Alice and Bob, and if they are equal, prints this result to the screen, and if not, prints Not Equal. Of course, they are not equal, so the output here is Not Equal.

What these two examples show is the leap that was taken to prove how easy code could be if we compare machine code and assembly code.

In Chapter 1, Introduction to Computer Programs we saw a program that was first written in machine code and then in assembly that printed the text Hello, World to the screen. What would that program look like in some of the high-level languages that we use today? Let's look at some examples.

In Python, it would look as follows:

print("Hello, World")

In C, it looks as follows:

#include <stdio.h>

int main(void)

{

  printf("Hello, World");

  return 0;

}

In C++, we have the following:

#include <iostream.h>

int main()

{

    std::cout << "Hello, World" << std::endl;

    return 0;

}

In Java, we would see the following:

class HelloWorld {

  static public void main( String args[] ) {

    System.out.println("Hello, World");

  }

}

In C#, we have the following:

class HelloWorld

{

    static void Main()

    {

        System.Console.WriteLine("Hello, World");

    }

}

Finally, in JavaScript, we would observe the following:

console.log("Hello, World");

We can see that they all are different and that some have some extra stuff surrounding the part that prints the text, but this comparison makes clear that the step from machine code is huge.

This step clears the path for several different ways to organize and structure code, and since the advent of the first high-level programming languages in the 50s, we have seen tremendous development. Right up to today, a vast amount of languages have been developed.