0% found this document useful (0 votes)
7 views27 pages

Chap3-Conditional Structure

The document provides an overview of conditional structures in programming, detailing various forms such as simple, alternative, nested, and multiple conditions. It includes syntax examples and flowcharts to illustrate how these structures function, along with practical examples and exercises for better understanding. Additionally, it discusses the translation of these structures into C programming language syntax.

Uploaded by

mr sebaagamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views27 pages

Chap3-Conditional Structure

The document provides an overview of conditional structures in programming, detailing various forms such as simple, alternative, nested, and multiple conditions. It includes syntax examples and flowcharts to illustrate how these structures function, along with practical examples and exercises for better understanding. Additionally, it discusses the translation of these structures into C programming language syntax.

Uploaded by

mr sebaagamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Yahia Fares University of Médéa

Faculty of Sciences
Department of Mathematics and Computer Science
1st Year Computer Science Licence Degree (L.M.D)

Algorithms and Data structure 1

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

If condition then action ; False True


Condition

action

Note1 : If condition is False , no action


will be executed
4
1-Simple form
Condition=expression
• The condition is a simple boolean expression or a sequence of logical
expressions (compound condition).

• A simple condition is made up of a single comparison operator (=, <>, <, ≤, >,≥)

• A compound condition is a condition made up of several simple conditions


linked by logical operators: AND, OR and NOT.

Condition = cond1 and cond2 or cond3...


E.g. : (X> 40) and (Y<20) or (X = Y) is a compound condition
5
1-Simple form

Expression

Comparison Logical

If x > 10 Then write If age >= 18 If is_valid AND


("x is greater than Then write ("You is_authenticated Then write
10“) ; are an adult“); ("Access granted“);
6
1-Simple form
Example

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
XX*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:

False Condition True

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:

If condition1 then If condition1 then Action1


If condition2 then else If condition2 then Action2
…. else
if conditionn then ….
…. If conditionn-1 then Actionn-1
else Actionn

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

False expression=value True


n

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 : has infinite x = -b/a


number of real solutions
(Set=R)

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)

not allowed for a float

27

You might also like