Chap2_variables_Algorithms
Chap2_variables_Algorithms
The first thing to do before you can use a variable is to create a box and stick a
label to it. This is done at the very beginning of the algorithm, even before the
instructions. This is called the declaration of variables.
The name of the variable (the label of the box) follows certain rules depending
on the programming language. However, an absolute rule is that a variable name
can have letters and numbers, but excludes most punctuation signs, it cannot
contain spaces. A correct variable name also begins by a letter.
In algorithm, some of these rules still apply. So a correct variable name
should not begin with a number;
1
Chapter2: Variables, Read/Write Instructions Lesson Notes by Peupie Guy Bertrand
Academic year 2022/2023 ISTAG OMNISPORT CAMPUS
Data type: Name of Variables separated by comma (if there are many
variables for that data type)
Example:
Integer: a, b, s, pos;
2
Chapter2: Variables, Read/Write Instructions Lesson Notes by Peupie Guy Bertrand
Academic year 2022/2023 ISTAG OMNISPORT CAMPUS
Real: res;
This means that we are declaring one variable called res, and whose type is real.
In the course of the program, the values of the variables changes. When we
attribute a value to a variable, in the algorithmic jargon, we say we are assigning
a value to the variable. When we declare a variable and assign a first value to
that variable before we start using it, we say we are initializing the variable. So
initializing a variable is given the variable its first value before any other
instruction in the program gets to change it. As we move further, we will see
cases in which initializing a variable is of upmost importance.
In algorithms, variable assignment is done with the use of the symbol.
The left hand of the assignment operator must be a variable and the right hand
must be a value or expression which when evaluated produces a value. Both left
hand and right hand should be of the same data types.
Example:
Let’s consider the following extract of an algorithm
Integer: A, B;
A 10;
B A;
At the end of these two instructions the value of A and B are both 10. But note
should be taken that both variables did not get the value ten at the same time. A
got it first. Also note that the value of A does not change after the second
instruction. The assignment operator modifies only the content of what is at the
left of the symbol.
3
Chapter2: Variables, Read/Write Instructions Lesson Notes by Peupie Guy Bertrand
Academic year 2022/2023 ISTAG OMNISPORT CAMPUS
Example:
Name "Sandra";
Surname Name;
Note: In algorithms, the order of instructions is of upmost importance given that
the computer executes the instructions one after the order following the order in
which the instructions are written.
Exercises
a) What will be the value of the variables after executing each set of
instructions below? Note that the symbol + is used for addition and - for
subtraction.
4
Chapter2: Variables, Read/Write Instructions Lesson Notes by Peupie Guy Bertrand
Academic year 2022/2023 ISTAG OMNISPORT CAMPUS
A computer program needs at some point in time to interact with the user either
by asking the user to enter some input it needs or it could be to display the
results obtained at the end of the program. Each programming language has its
own in-built functions used for these various interactions. In algorithms, we
have two instructions that permit us to interact with the user. These instructions
are the Read and Write instructions.
The write instruction allows the computer program to write a message or the
value of a variable on the computer screen. To write a message simple put the
message in quotes and to write the content of a variable, simply write the
variable name.
Example: Write an algorithm that asks a user to enter his name, and the name is
written on the screen.
TO-DO
5
Chapter2: Variables, Read/Write Instructions Lesson Notes by Peupie Guy Bertrand
Academic year 2022/2023 ISTAG OMNISPORT CAMPUS
Exercise: Modify the algorithm above such that it writes good morning before
writing the user’s name. For example, if the user types, Guy Bertrand, the
algorithm should print as result Good morning Guy Bertrand.
Note: As a convention we will use the empty instruction Write() to go to a new
line.
Homework
1) Give the value of each variable at the end of execution of algorithm
below.
Algorithm something1 Algorithm something2
Integer: A,B,C,D,X; Integer: A, B, aux;
start start
A ← 3; A ← 5;
B ← -5; B ←2;
C ←A*B; Aux ←A;
D ← C+10; A ←B;
X ←D+A; B ← aux;
B ←X+D; end
A ← B –C;
end
A= D= A=
B= X= B=
C= aux
2) Write an algorithm that ask to the user three (3) numbers and perform the
arithmetic mean and display the result.