0% found this document useful (0 votes)
3 views

Chap2_variables_Algorithms

Uploaded by

noudjemambe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chap2_variables_Algorithms

Uploaded by

noudjemambe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Chapter2: Variables, Read/Write Instructions Lesson Notes by Peupie Guy Bertrand

Academic year 2022/2023 ISTAG OMNISPORT CAMPUS

CHAPTER 2: VARIABLES, READ/WRITE INSTRUCTIONS

Objectives of this Chapter


At the end of this chapter, you should be able to:
 List the various data types in algorithms;
 Correctly declare a variable;
 Explain the difference between initializing a variable and assigning a value to a
variable;
 Execute an algorithm and find out what it does;
 Write algorithms that interact with the user.

1. What is the use of Variables?

In a computer program, we will constantly need to store values manipulated in


the program. This may be data from the hard disk, data provided by the user
(typed in) or from other sources. It can also be the results obtained by the
program, intermediate results or final ones. These data may be of different types
(we will talk about it as we move ahead): they can be numbers, text, etc. .
Whatever the case, as soon as you need to store information during a program,
you use a variable.
You can imagine a variable to be a container, which the program (via the
computer) is going to locate by a label. To access the content of the container,
you just need to indicate its label.
1.1. How to Name Variables

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

 should not have space;


 should not contain punctuation signs and special symbols except the
underscore character(_).
When declaring a variable, it isn’t enough to simply create a container, you
must precise what will be stored in that container. This is because information is
represented differently in the machine depending on the types. Numbers and
letters aren’t stored in the same manner. So precising the data type enables the
computer to know the actual size of the data to allocate for your variable.
1.2. Types of Variable

Each programming language offers a wide range of data types. In algorithms, we


will limit ourselves to the most basic once that can be seen in most programming
languages.
- Real: This variable type is used to store real numbers.
- Integer : This variable type is used to store whole numbers
- Character : This variable type is used to store a single letter, or punctuation
mark, special symbol
- String: It is used to store a group of characters.
- Date: It is a special data type used to handle dates. It stores the day, month
and year.
- Boolean: It is used to store data that can have only two possible values.
Example: TRUE /FALSE, YES/NO, ON/OFF.
Note that the Boolean data type is storage economic since it uses just a single bit
to represent the two values.

1.3. Variable Declaration


Now that we know what it takes to declare a variable, let’s look at how it is done
practically. Variables are declared according to the syntax shown below:

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

This means that we are declaring four variables of type integer.

Real: res;
This means that we are declaring one variable called res, and whose type is real.

1.4. Variable Assignment

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

NB: When assigning a string to a variable, we put the string in quotes.

Example:

String: Name, Surname;

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.

A←5 A←5 A←3


B←A+4 B←2 B ← 10
A←A+1 A←B C←A+B
B←A–4 B←A B←A+B
A←C
A= A= A=
B= B= B=
C=

b) Write an algorithm that permutates the content of two variables A and B.


Write the above algorithm to three variables instead A, B and C
Can you draw a conclusion given N variables to permutate?

4
Chapter2: Variables, Read/Write Instructions Lesson Notes by Peupie Guy Bertrand
Academic year 2022/2023 ISTAG OMNISPORT CAMPUS

2. Read / Write Instruction

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.

2.1. Read Instruction

The Read instruction enables the program to collect user input.


Its syntax is as follows:
Read (var) ; where var is a variable name.
It reads the input typed by the user on the keyboard and stores it in the variable
given to it.

2.2. Write Instruction

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.

You might also like