Chap3-Conditional Structure
Chap3-Conditional Structure
Faculty of Sciences
Department of Mathematics and Computer Science
1st Year Computer Science Licence Degree (L.M.D)
2024/ 2025
1
Chapter III
Conditional structure
2
Conditional structure
In some situations, instructions are only carried out if a certain
conditions are verified. This is called conditional statement or
conditional structure.
It allows to perform tests and select the instructions to execute
following the result of a condition evaluation.
There are several forms of conditional structures :
➢ Simple
➢ Alternative
➢ Nested
➢ Multiple 3
1-Simple form
• Allows to execute of an action (simple or composite) if the result obtained
from the evaluation of a condition (logical expression) is True
Syntax : Flowchart
action
• A simple condition is made up of a single comparison operator (=, <>, <, ≤, >,≥)
Expression
Comparison Logical
Read (age);
If age >=18 then write (’’You are adult’’);
7
1-Simple form
Note2 : If Action consists of several instructions (2 or more), they
must be put in a block delimited by "Begin" and "End".
Syntax :
If condition Then
Begin
Action1;
Action2;
…
Actionn;
End ;
8
1-Simple form
Example :
If X>10 Then
Begin
XX*2;
Read(Y);
Write(X+Y);
End ;
9
2-Alternative Form
It allows the execution of actions (simple or compound) based on
the result obtained from the evaluation of the condition, we have
two possibilities :
1. if the condition is True, the first action will be executed
2. if the condition is False, the second action (alternative) will be
executed.
Syntax :
if condition then Action1 ;
else Action2 ;
10
2-Alternative Form
Flow diagram:
Action2 Action1
11
2-Alternative Form
Example:
Read (a);
if a = 0 then Write (a is null) ;
else Write ( a is not null) ;
12
2-Alternative Form
Exercise : Write an algorithm that, based on an amount, displays the amount
to be paid, knowing that a 10% discount is deducted for any amount greater
than or equal to 3500 DZD.
Solution 1
Algorithm paid_amount ;
Const disc = 0.1 ;
Var amount, discount : Real;
Begin
discount 0 ;
Read(amount);
If amount>= 3500 Then discount amount * disc;
Write (amount - discount) ;
End. 13
2-Alternative Form
Solution 2
Algorithm paid_amount ;
Var amount , amouP : real;
Begin
Read (amount) ;
If amount>= 3500 then amouP amount*0.9 ;
else amouP amount;
Write (amouP) ;
End. 14
3-Nested Form
A nested conditional structure is a conditional statement that is contained
within another conditional statement (If or else). This allows to test multiple
conditions and execute different actions depending on the results of the tests.
Syntax 1: Syntax 2:
15
3-Nested Form
Example :
Write(Enter your score );
Read (a) ;
if a>=10 then
if a < 16 then Write(Good) ;
else Write (excellent) ;
else Write(Not good) ;
16
4-Multiple form
Multiple condition (or multi-way decision) statement, Switch allows to execute
actions among many alternatives, depending on the value of an expression.
This is similar to a multiple if-else statement but is more efficient and easier to
read and write for cases with many possible values.
Syntax:
Switch expression
begin
case value1 : action1 ;
case value2 : action2 ; Note1 : The default statement is optional.
… Note2 : Value1 … Valuen must be from the
case valuen : actionn same type as expression.
default : actionn+1 ;
end ; 17
4-Multiple form
Flow diagram
False True
expression=value1
Action1
False True
expression=value2
Action2
Actionn+1 Actionn …
18
4-Multiple form
Example :
Algorithm Display_day;
Var day : integer ;
Begin
Read (day) ;
Switch day
begin
case 1 : Write (Sunday) ;
case 2 : Write (Monday);
case 3 : Write (Tuesday) ;
case 4 : Write (Wednesday) ;
case 5 : Write (Thursday);
case 6 : Write (Friday);
case 7 : Write (Saturday)
default : Write (No matching day);
end;
end. 19
Exercise
• Write an algorithm to solve a first-degree equation: ax + b = 0
Analysis
Case1 : Case 2 :
a=0 a≠0
b ≠ 0 : No solution 20
Solution
Algorithm Sol_1st_deg_eq ;
Var x, a, b : real ;
Begin
Write (Enter a and b :);
Read (a, b) ;
if a = 0 then
if b = 0 then write (Infinite number of real solutions) ;
else write (There is no solution) ;
else begin
x -b/a ;
write (The solution is : , x) ;
end ;
End.
21
Translation into Language
22
Translate in C: (if)
23
Translate in C: (if-else)
24
Translate in C: (Nested if-else)
25
Translate in C: (Switch case)
• break : allows to exit switch statement. If there is no ’break’ after printf then subsequent expression
will also get evaluated until we reach the next ’break’.
• default : is executed if no case value is equal to the value of x 26
Translate in C: (Switch case)
27