Algorithmic I Ch 3: Conditional structures – 1) if...
else 2023-2024
Conditional Action :
The conditional action is an instruction that makes it possible to condition the execution of a
sequence of instructions on the value of a boolean expression (condition).
Example :
When solving a quadratic equation, the value of the determinant Delta is calculated, and
depending on the value of the latter there are three actions to consider :
1) Cass Delta˂0 : We display no solution
2) Case Delta=0 : We calculate the value of one
resulat and we display it
3) Case Delta˃0 : We calculate the values of two
results, then we display them
Syntax :
The condition is evaluated. If true, actions 1 of the then clause are executed. Otherwise, actions 2 of the
otherwise clause are executed. In both cases, the algorithm continues after the ‘EndIf’.
Case 2 describes a simplified form of the conditional, without an else clause.
Example 1 : Display the sign of an integer number x.
if (x˃=0) then
Print(ˮx is positiveˮ);
else
Print(ˮx is negativeˮ);
endif;
Example2 : make an integer n even, if it is not so.
if (n mod 2<>0) then
n n*2 ; /*or :
nn+1 ;*/
endif ;
Page 1 sur 1