How does a computer program work?
A program's behavior depends on the type of language used to write the program.
Nonetheless, almost all programming languages are based upon the same principle:
The program consists of a set of commands that the computer must execute. The computer executes the commands as it
reads the file (from top to bottom) until it reaches a command (often referred to as a branch command) which
tells the computer to go to a specific location in the program. In a way it resembles a treasure hunt where the
computer must follow the clues and execute commands until it reaches the end of the program and is stopped.
The concept of variables
Variables are used in most languages, where a name is associated with content. For example, we can
call a variable "dummy" and assign it the value 8.
Some languages allow you to associate any data type to a variable (either a whole number or a
character), such a language is called a non-typed language.
As can be seen in the
chapter data representation, the type of data sets the number
of bytes that the data is coded in, i.e. the amount of memory that this
data takes up as well as the format in which it is represented.
For this reason evolved languages
(C, Java) are typed languages, meaning that a variable is not only associated with a name but also a
data type, which should be specified when declaring the variable, i.e. when the variable name is written for
the first time you must specify in the compiler the type of data this variable will contain (the method of
declaring the variable depends on the language used).
Programming languages require strict syntax, you cannot simply write things as you wish.
Thus, some languages are case sensitive, meaning that a name written in lower case is
not considered to be the equivalent of the same name written in upper case. So the variable "Dummy" is
different to the variable "dummy".
Variable names usually require a maximum length (depending on the language) and a certain
character set, where the following characters are generally used:
So a space (" ") is therefore considered a different type of character, called a
special character. It is therefore not very common for a language to allow the use of special characters
in variable names!
In most languages there are a handful of words that may not be used as variable names, these
are called reserved words. These are specified in the chapter corresponding to the specific language.
A constant is data type whose value cannot be modified. These are generally defined at the beginning
of the program. The value of the constant may be of any type permitted by the programming language.
It is useful when writing a program to be able to add lines of text that the compiler does not treat as code.
These lines of text are usually preceded (or enclosed) by special commands which tell the compiler
to ignore them.
Comments are used to clarify how the program is written by explaining parts of the code.
Comments are useful if another person is trying to understand how the program works by reading the source file,
or even, if the person who wrote the program is reading the source file again some years after having originally
written the code.