Professional Documents
Culture Documents
EE121
FALL 2022
Dr. R. NAMANE
What is computer solving problem?
START
Action 1
Body Action 2
Action n
STOP
Variables
Typically, an algorithm consists of instructions that
tell the computer what to do and data that the
algorithm uses.
The data consists of constants or fixed values that
never change and variables or variable values that
may vary during the execution of the program.
A variable may refer to an input data, output
data, or intermediate data.
A variable is a named memory location in
RAM that holds a value.
Variables
In an algorithms:
All variables must be declared before they
can be used!
Every variable has:
• Name (Identifier)
• Data type
• Value
Variables Names (Identifiers)
A variable's name is known as an identifier. The identifer
given to a variable usually follows certain rules:
It can contain letters and numbers but must start with a
letter.
It must contain at least one letter (at the start of the name).
It must not contain special characters such as !@£$%&* or
punctuation characters. However, an underscore can be
used. Spaces are not allowed.
It should not be a reserved word. Examples of reserved
word: if, for, while, etc
The name should be meaningful - choose variable names in
relation with the given problem.
Examples of variable names: Nbr1, Nbr2, Value1, Value2, Sum,
Average, Surface,Total_price, …etc
Variables data types
Each data type prescribes and limits the form of the data,
usually limited in length.
Different data types have different sizes in memory
depending on the machine.
Commonly used data types are:
Integer
Real (Floating-point)
Character
String
Boolean
Integers
An integer is a number that has no fractional part.
There are often different sizes of integers available; for example, PCs
support short integers, which are 2 bytes, and long integers, which are 4
bytes.
The range of values for short integers is [-215,+215[ ([-32768,+32767[ ).
The range of values for long integers is [-231,+231[.
Operations used on integers are:
The four arithmetic operations: + , - , * , /
Comparison operators: =, !=,<, <=, >, >=
The division / is an integer division. Ex : 11/4 =2 and not 2.75 !)
There exist a modulus operator %, which gives the remainder of an
integer division. Ex : 11%4=3
Real (floating-point)
It a number with a decimal point that separates the integer and
fractional parts.
Two floating-point formats exist: single and double precision.
Single precision uses 4 bytes (6 digits of precision order).
Double precision uses 8 bytes (14 digits of precision order).
Operations used on integers are:
The four arithmetic operations: + , - , * , /
Comparison operators: =, !=,<, <=, >, >=
The division / gives a decimal result. Ex : 11/4 =2.75 )
The modulus operator % doesn’t exist.
Characters
In graphics-based applications, the term character is generally
reserved for letters, numbers, and punctuation..
It only can represent single character.
The following are characters: a, A, @, $, 2,..
By convention, characters are surrounded with single quotes.
Examples: ‘a’, ‘B’, ‘5’..
A character is data type that takes 1 byte. This byte
corresponds to the ASCII code of that character. Example,
the ASCII code of letter ‘A’ is 65 in decimal and 01000001 in
binary.
Operations used on characters are:
Comparison operators: =, <, >, ..
String
String is a sequence or a series of characters.
By convention, strings are surrounded with double quotes.
Examples: “Boumerdes City”, “Mohamed Amine”,
“Computer”,..
Operations used on strings are:
Concatenation operators: & (for joining multiple strings
into one string).
Boolean
This data type only have two possible values:
–True
– False
Use this data type for simple flags that track true/false
conditions.
Principal operations used on booleans are:
Logic operators: AND, OR, NOT, ..
Truth Table for Logical Operators:
A B A AND B A OR B NOT A
False False False False True
False True False True True
True False False True False
True True True True False
How to declare variables?
Recall: All variables must be declared before they
can be used!
Variables are declared as follows:
Variables list of identifiers : type
Examples:
Variables i, j, k : integer
x, y : real
OK : boolean
Str1, Str2 : string
Decision (Choosing)
Repetition (Looping).
1. Sequence
It is made by a sequence of statements placed one
after the other.
The instruction above or before gets executed
first.
The main instructions used here are:
Read instruction
Write instruction
Assignment operation and expressions
to manipulate data
Read instruction
To accept data from the user, we use Input or Read instruction.
Examples:
− Read (A) is an instruction to ask the user to introduce a value for
variable A.
− Read (V1, V2, V3) is an instruction to ask the user to introduce
three values for variables V1,V2, and V3 respectively.
Remarks:
− The algorithm stops when it encounters a Read instruction and it does
not move to the next instruction until the user enters the requested data
from the keyboard followed by pressing the enter key to denote the end
of the input.
− Before reading the value of a variable, it is strongly advised to write
messages on the screen, in order to tell the user what to introduce.
Write instruction
To display data for the user, we use Print or Write
instruction.
Examples:
− Write (Sum) is an instruction to display the value of the
variable Sum on the display device.
− Write (X1, X2) is an instruction to display the values of the two
variables X1 and X2 on the display device.
− Write (“Hello World”) is an instruction to display the message
“HelloWorld” on the display device.
− Write (“The Sum is: ”, Sum) is an instruction to display the message
“The Sum is: ” followed by the value of the variable Sum on the
display device.
Expressions
An expression is a mathematical phrase that can
contain ordinary numbers, variables (like x or y) and
operators (like add, subtract, multiply, and divide).
− Examples: a+1, a-b, (3*x-y)/2.