if - else & if - elseif - elseif - ….. else & nested if structures:

A more complex form of the selection structures is given by if - else . Look at the following diagram:

Block #1

if (Condition )
{

Block #2
}
else
{
Block #3
}
Block #4



Sequence of statement execution:

Block #1 will be executed

The condition of the if statement will be checked

If the condition is true then block#2 will be executed and then block#4

If the condition is false then block#3 and then block#4 will be executed

 

Even more complex structures can be obtained by the use of if - elseif - elseif - ….. else
See the following diagram:

Block #0

if (Condition #1 )
{

Block #1
}
else if (Condition #2 )
{
Block #2
}
else if (Condition #3 )
{
Block #3
}
else
{
Block #4
}
Block #5



Sequence of statement execution:

Block#0 will be executed

Condition#1 of the if statement will be checked

If condition#1 is true then block#1 will be executed and then we will jump to block#5

If condition#1 is false then condition#2 will be checked

If condition#2 is true then block#2 will be executed and then we will jump again to block#5

If condition#2 is false then condition#3 will be checked

And so..on and so..on

If all conditions are false then block#4 will be executed and then block#5

Jacob Y. Kazakia © 2001 All rights reserved