Control Structures in Algorithms
Control Structures in Algorithms
The structures of
basic control
If<<condition>>Then V F
Instruction 1 Condition
Instruction 2
....... Instruction(s)
Instruction N
End Yes.
Instruction k
Instr_after
If the condition is verified that is to say having the value True, the block
Instruction(s) will be executed otherwise will be ignored.
Conditional structure
Correction activity 01 with the simple alternative
Algorithm: Competition
VariableMoy:Réel
Start:
Write("Result of the competition");
Write("Enter your average")
Read(Average);
If (Moy>=14) Then
This student is accepted to the competition
End If
Fin.
Conditional structure
Simple structure:
Exercise 01:
Write an algorithm that asks the user for their name and their field of study.
then display the message "hello <the name>". If the field is "info",
the algorithm displays a "welcome" message
Conditional structure
Correction exercise 01 with the simple alternative
Algorithm: pathway
Variable, major: string
Start:
Write("Enter your name")
Read(name)
Write("Enter your field")
Read(major)
If (major='info' or major='INFO') Then
Write("Hello", name, "Welcome")
Fine Yes
Fin.
Conditional structure
Alternative Structure:
Activity 01:
Write the algorithm that calculates the quotient of two
variables A and B of real type.
Conditional structure
Correction Activity 01
Algorithm: Quotient
VariableA, B, Quotient :Réel
Start:
Quotient of two numbers
Write("Enter the value of A");
Read(A);
Write("Enter the value of B")
Quotient A/B;
Write(ʺThe result is:ʺ, Quotient);
End.
Conditional structure
Alternative structure:
Syntax:
If<<condition>>Then
Instruction 1;
....
Otherwise
Instruction 2;
......
End Yes.
Instruction 3
If the condition is met, meaning it has the value True, the instruction block 1 will be executed.
and instruction block 2 will be ignored.
Otherwise, instruction block 1 will be ignored and instruction block 2 will be executed.
Conditional structure
Correction Activity 01 with the structure If ... Then Else ... End If
Algorithm: Quotient
VariableA, B, Quotient :Réel
Start:
Write ("Quotient of two numbers")
Write("Enter the value of A");
Read(A);
Write("Enter the value of B")
If (B=0) Then
Write("Impossible division");
Otherwise
Quotient A/B;
Write("The result is:", Quotient);
End If
End.
Conditional structure
Exercise 01:
Write an algorithm that calculates the maximum of two numbers A and B
of type Integer.
Exercise 02:
Write an algorithm that calculates the minimum of two numbers A and B
of integer type.
Exercise 03:
Write an algorithm that displays the message 'not admitted' if the grade of
The student is failing if the grade is below 10 and passed if the grade is above 10.
Exercise 04:
write an algorithm that displays the absolute value of a number N
real type.
Conditional structure
Imbrication of conditional structures:
Activity: sign of an integer
Write an algorithm that asks the user for an integer and
It then reports if this number is positive, negative, or zero.
Conditional structure
Alternative non-nested Nested alternative
Algorithm:Sign_number Algorithm:Sign_number
Variables: integer Variables : integer
Start : Start:
Write("Enter a number N") Write("Enter a number N");
Read(n); Read(n);
If (n>0) Then If (n>0) Then
Write("Positive number"); Write(" Positive number");
End If Otherwise
If (n<0) Then If (n<0) Then
Write(" Negative number"); Write(" Negative number");
End Yes Otherwise
If (n=0) Then Write(" Null number");
Write(" Null number"); End If
End If End If
End. End.
Conditional structure
Imbrication of conditional structures:
Conditional structures can be nested (that is to say included)
together with each other).
Syntax:
Conditional structure
Exercise 01:
Write an algorithm that gives the state of water according to its temperature:
- If T <= 0: it is ice.
If T > 100: it's steam.
If 0 < T < 100: it is liquid.
Exercise 02:
Write an algorithm that displays the day of the week in full.
according to his number (1-7).
Conditional structure
Correction exercise 01
Algorithm: Water State
VariableT :réel
Start :
Write("Enter the water temperature T")
Read(T);
If (T<=0) Then
It's ice.
Otherwise
If (n<=100) Then
It's liquid
Otherwise
It's steam
End If
End If
End.
Correction exercise 04
Algorithm: Day_of_the_week
Variable_num_day :integer
Start:
Ecrire(ʺEntrez le numéro du jourʺ);
Read(day_number);
If (day_number=1) Then
Write("Monday");
Otherwise
If (day_number=2) Then
Tuesday
Otherwise
If (num_day=3) Then
Wednesday
Otherwise
If (num_day=4) Then
Thursday
Otherwise
If (day_number=5) Then
Friday
Otherwise
If (day_number=6) Then
Write("Saturday")
Otherwise
Sunday
End Yes
End If
End If
End Yes
End If
End If
End.
Conditional structure
Multiple choice structure:
In the case where a variable can take multiple values and depending on
for each value we perform a process we use the choice structure
multiple.
Syntax:
According to <<Selector>> do
Valeur1: instruction1
Valeur2: instruction2
.......
ValeurN : instructionN
otherwise: default_instruction
Next Finish.
Conditional structure
Correction exercise 04 with multiple choice structure
Algorithm: Day_of_the_Week
Variable_num_day: integer
Start :
Write(ʺEnter the day numberʺ);
Read(day_number);
According to num_day do
1: Write("Monday")
Write("Tuesday")
Write("Wednesday")
Thursday
Friday
Write("Saturday")
Write("Sunday")
Otherwise: Write ("invalid day")
End According to
End.
Alternative structure
Exercise 01:
Write an algorithm that displays the month in full according to its number.
number (1-12).
Conditional structure
Exercise 02:
Write an algorithm that inputs a grade and displays the mention according to it.
Notes Mentions
the table below.
Note less than 10 Postponement
10 <= Grade < 12 Passable
12 <= Note < 14 Quite well
14 <= Note < 16 good
16 <= Note < 20 Very well
Note = 20 perfect