Flow of Control

Pseudo Code

Control Structures

There are three types of flow of control

  • Sequential flow
  • Selection or Conditional flow
  • Iterative flow

Sequential Flow

Steps of the algorithm is executed one by one in a sequence.


statement 1
statement 2
statement 3
:
:

Example

Set sum = 0;
Read: x, y;
Set sum = x + y;
Print: sum;

Note! Above instructions are executed step by step

Selection or Conditional Flow

Selection flow are of three types

  • Single Alternative
  • Double Alternative
  • Multiple Alternative

Selection Flow – Single Alternative

Single Alternative


if expression then
	statements
endif

Example

if x == 10 then
	Print: “TEN”;
endif

Note! If the expression is TRUE then the statements inside the body of the if is executed otherwise it is ignored.

Selection Flow – Double Alternative

Double Alternative


if expression then
      statement-A
else
      statement-B
endif

Example

if x == 10 then
      Print: “TEN”;
else
      Print: “x is not TEN”;
endif
Note! If the expression is TRUE then statement-A is executed otherwise statement-B is executed.

Selection Flow – Multiple Alternative

Multiple Alternative


if expression-1 then
      statement-1
else if expression-2 then
      statement-2
:
:
else if expression-n then
      statement-n
else
      statement-last
endif

Example

if x == 1 then
      Print: “ONE”;
else if x == 2 then
      Print: “TWO”;
else if x == 3 then
      Print: “THREE”;
else
      Print: “x is not 1, 2 and 3”;
endif

Iterative Flow

Iterative flow are of three types

  • for loop
  • while loop
  • do-while loop

Iterative Flow – for loop

Method 1


for i = r to s by t do
	statements
endfor

Example

for i = 1 to 5 by 1 do
	Print: i;
endfor

This will print 1 2 3 4 5

Method 2


for (i = r; i <= s; i++)
	statements
endfor

Example

for (i = 1; i <= 5; i++)
	Print: i;
endfor

This will print 1 2 3 4 5

How for loop works


for i = r to s by t do
	statements
endfor

Algorithm in simple English

1.	Initialize i = r   (PROCESS)
2.	Is i <= s  (DECISION)
		if YES then

			Execute statements

			Calculate i + t and assign it to i

			Repeat Step 2   (PROCESS)

		else

			Stop

Iterative Flow – while loop


while expression do
	statements
endwhile

Example

Set i = 1;
while i <=5 do
	Print: i;
	Set i = i + 1;
endwhile

This will print 1 2 3 4 5
Note! In while loop the expression is checked first and if it is TRUE then the statements inside the loop is executed otherwise it is ignored.

Iterative Flow – do-while loop


do
     statements
while expression

Example

Set i = 1;
do
    Print: i;
    Set i = i + 1;
while i <= 5

This will print 1 2 3 4 5
Note! In case of do-while first the body of the loop is executed then the expression is checked. So with do-while you are guaranteed that the body will be executed at least once.