You are on page 1of 2

Computer Programming

A computer program consists of a sequence of instructions, in some programming language (like Python
or C++), which is to be executed by a computer. These instructions are executed one by one, from
top to bottom.
A computer program in human-readable form is called source code. Source code is automatically
translated into so-called machine instructions, which can be executed by the hardware of a computer.
Here some basic principles of source code are explained, containing only tiny examples. Of course
in practice computer programs can be huge, containing hundreds of thousands of lines of code, and
driving complicated applications like luggage transport systems on airports or Internet services.
Source code typically consists of assignments of values to variables. For example, x := 1; assigns
the value 1 to the variable x. The semicolumn ; signifies the end of the assignment. A tiny example
of a piece of source code is

x := 2;
x := x + 1;

The first instruction, on the first line, assigns the value 2 to the variable x. In the second instruction,
on the second line, x occurs twice. At the left-hand side of this instruction it represents the variable x
to which the new value is assigned. At the right-hand side of this instruction it represents the current
value of x before this assignment, i.e., 2. So this instruction assigns the value 2 + 1 = 3 to the variable
x at the left.
Variables tend to be typed, defining precisely the range of possible values a variable can attain. For
example, if a variable has type integer, it can carry integer values like 3, 0 and -5, but not say a word
like ‘dragon’. Another example is that a variable can have the type ‘list of integers’, meaning that it
can carry as values lists like [2,-3,7], or the empty list [].
Integers and lists of integers are examples of data types. On such data types, operations can be
defined, such as addition or multiplication on integers, or concatenation on lists. For example, the
concatenation of lists [2,-3,7] and [4,2] is [2,-3,7,4,2].
A second example of a piece of source code is

x := 3;
y := 1;
z := x + y;

After executing this computer program, x, y and z have the values 3, 1 and 4, respectively. (Declara-
tions of types of variables are omitted from the examples.)
Source code can also contain repetitions of instructions. Notably, the for loop for i in 1 to n:
Procedure performs the block of source code Procedure n times, whereby the variable i consecutively
has the value 1, 2, . . . , n. For example, consider

x := 0;
for i in 1 to 3:
x := x + i;

In the first line, variable x is assigned the value 0. Next the line x := x + i in the for loop is executed
three times, where i has the values 1, 2, 3. In the first iteration, where i is 1, x is assigned the value
of x plus 1, i.e., 0 + 1 = 1. In the second iteration, where i is 2, x is assigned the value of x plus 2,
i.e., 1 + 2 = 3. Finally, in the third iteration, where i is 3, x is assigned the value of x plus 3, i.e.,
3 + 3 = 6.
For loops can also be nested in each other. For example, consider

1
x := 0;
for i in 1 to 2:
for j in 1 to i:
x := x + j;

The line x := x + j in the for loop is executed three times: once in the outer for loop where i is 1,
with j equal to 1 in the inner for loop; and twice in the outer for loop where i is 2, with j equal to
1, 2. Concretely, in the first outer iteration, i is 1. Then the inner for loop is executed only for j is 1,
where x is assigned the value of x plus 1, i.e., 0 + 1 = 1. In the second outer iteration, i is 2. Then
the inner for loop is executed for j is 1 and 2. In the first run, where j is 1, x is assigned the value of
x plus 1, i.e., 1 + 1 = 2. Finally, in the second run, where j is 2, x is assigned the value of x plus 2,
i.e., 2 + 2 = 4.
A while loop while condition: Procedure repeatedly executes the block of source code Procedure,
as long as condition remains true. For example, consider

x := 0;
y := [];
while x < 3:
y := insert(x,y);
x := x + 1;

Here the variable x has type integer, while the variable y has type list of integers. The operation
(also called function) insert has two arguments: the first argument is an integer while the second
argument is a list of integers. It inserts its first argument at the beginning of its second argument.
For example, insert(-7,[2,-3]) produces the list [-7,2,-3].
The while loop above inserts three values in the list y, which initially is empty. In the first iteration,
where x has the value 0 (which is smaller than 3), 0 is included at the start of the empty list, producing
[0] as value for y. At the end of this iteration, x is increased to 1. In the second iteration, where x
has the value 1 (which is smaller than 3), 1 is included at the start of the list [0], producing [1,0]
as value for y. At the end of this iteration, x is increased to 2. In the third iteration, where x has
the value 2 (which is smaller than 3), 2 is included at the start of the list [1,0], producing [2,1,0]
as value for y. At the end of this iteration, x is increased to 3. Finally, the fourth iteration, x has
the value 3, meaning that the condition x < 3 of the while loop no longer holds, so the while loop
terminates.
Note that in principle a while loop may be executed an infinite number of times, if its condition
stays true forever.

You might also like