You are on page 1of 11

1

CONTROL STRUCTURES

In the very simplest of programs, instructions are executed in the order in which they appear,
each instruction being executed once and only once. in most cases however, pro9grams require
looping and selection. To achieve this we need control statements.

There are five main control structures in C.


 Selection statements
a) If…….statement
b) If….else…statement
c) Switch….statement
 Looping statements
d) While….statement
e) Do….while…. statement
f) For…statement
a) if….statement
Generally the if…statement allows selection to be made on two possible outcomes.
The general form of the if…statement is

if (test expression)
statement;
if the test expression is true, the statement following if…is executed. Otherwise it is ignored

example:-
/* program to illustrate if…construct*/
#include<stdio.h>
Void main()
{
int i;
printf(“/n Enter a number:”);
scanf(“%d”,&i);
if(i%3= =0)
printf(“\n%d is divisible by 3.”,i);
else
printf(“\n%d is not divisible by 3.”,i);
}

b) if….else…statement
This is an extension of the if…statement in (a).
The general form is
if(test expression)
{
Statement1;
}
else
{
2

Statement 2;
}
If the expression is evaluated to be true, the statement that comes immediately after if…are
executed. Otherwise the statements that follow else…. are executed.

/*program to illustrate the if…else…statement*/

#include<stdio.h>
Void main()
{
int i;
printf(“\enter a number:”);
scanf(“%d,&i);
if(i%3= =0)
print f(“%d is divisible by 3.\n”,i);
else
print f(“%d is not divisible by 3.\n”,i);
}

Test false
false Test
expr
expre
essi
ssion
on

true true

Body if Body of if… Body of


if…. else…

Exit Exit
3

c) switch ….statement
The switch…statement allows selection to be made from several options. Only one option is
executed at a time.
The general form is
Switch (choice)
{
case label 1:
Statement;
Break;
case label 2:
Statements;
Break;
……..
……..
……..
default:
Statement;
}

The control variable choice can be char or int. if the value of choice is equal to the label 1,
the body of the first case is executed and if choice is equal to label 2 the body of the second
case is executed and so on.

Choic True
e=lab Body of first choice
el 1

False

True
Choic Body of second choice
e=
label
2

False
Body of default
4

exit

*/illustration of if….else…statement*/
#include<stdio.h>
Void main()
{
Int choice;
printfC”choice of destination:\”);
printf(“\1 - mercury\n”);
printf(“\2 - venus\n”);
printf(“\3 - mars\n”);
printf(“\enter the number corresponding to your choice:”);
scanf(“%d”,&choice);
if(choice= =1)
{
puts(“mercury is the closest to the sun”);
puts(“the weather may be quite hot there.”);
puts(“the journey will cost you Ksh 80000.”);
}
else
if(choice = = 2)
{
puts(“Venus is the second planet from the sun”);
puts(“the weather may be quite hot and poisonous”);
puts(“the journey will cost you Ksh70000.”);
}
else
if(choice = =3)
{
puts(“mars is the closest planet to earth”);
puts(“the weather has been excellent till now.”);
puts(“the journey will cost you Ksh 60000.”);
}
else
puts(“unknown destination.”);
puts(“\n note:Ksh = Kenya shilling.”);
}
5

/*program to demonstrate use of switch….statement*/


#include<stdio.h>
Void main()
{
int choice;
printf(“choice of destination:\n”);
printf(“\t 1.-mercury\n”);
printf(“\t 2.-venus\n”);
printf(“\t 3.-mars\n”);
printf(“enter the number corresponding to your choice:”);
scanf(“%i’,&choice);
switch (choice)
{
case 1:
puts(“mercury is the closest to the sun”);
puts(“the weather may be quite hot there.”);
puts(“the journey will cost you Ksh 80000.”);
break;

case2:
puts(“Venus is the second planet from the sun”);
puts(“the weather may be quite hot and poisonous”);
puts(“the journey will cost you Ksh70000.”);
break;

case3:
puts(“mars is the closest planet to earth”);
puts(“the weather has been excellent till now.”);
puts(“the journey will cost you Ksh 60000.”);
break;
default:
puts(“unknown destination.”);
break;
}
puts(“\n note:Ksh = Kenya shilling.”);
}
6

d) while…statement
The for….statement is used where the number of times the loop is executed is known in
advance. When the number of times the loop is to be executed is not known in advance, the
while…or do….while….statement are used
The general form of the while…statement is
while(test expression)
statement;
The general flow chart is shown below

Test false
expressio Exit
n

true

Body of loop
7

/*program to display 0, 1, 2, 3, 4, 5…9continuously*/


#include<stdio.h>
Void main()
{
int digit = 0;
while (digit<=9)
{
printf(“%d\n”,digit);
digit++;
}
}

e) do….while…statement
The while…loop is tested at the top,i.e, the test expression containing the condition to be
checked to decide whether to execute or not to execute the body of the loop is evaluated before
executing any of the statements in the body of the loop. As a result, the body of the loop is not
executed at all when the test expression is initially false.
The general form of the do….while….statement is
do
Statement;
while(test expression);
The test expression in the do….while…. statement appears at the end of the body of the loop.
The statement in the loop will always be executed at least once, since the test for repetition does
not occur until the end of the first pair through the loop.
The general flowchart and syntax of the do….while…loop is shown below
8

Body of loop

False
Test Exit
expressio
n

true

/*program using do….while…statement*/


#include<stdio.h>
void main()
{
int choice;
do
{
printf(“\n\t\t\t – TOURIST RESORT CENTERS”);
printf(“\n\n 1-NAIROBI NATIONAL PARK”);
printf(“\n\n 2 - TSAVO”);
printf(“\n\n 3 -AMBOSELI”);
printf(“\n\n 4 - EXIT”);
printf(“\n\n pls enter your selection [1 – 4]”);
scanf(“%d”,&choice);

switch(choice)
{
case 1:
printf(“\n be ready to see the”);
printf(“\n king of the jungle”);
break;
case 2:
printf(“\n beware of the”);
printf(“\n man eaters”);
break;
case 3:
printf(“\n be ready to see”);
printf(“\n the jumbo”);
9

break;
case 4:
printf(“\n good bye!”);
break;
default:
printf(“\n you have landed in a bush”);
printf(“\n without animals”);
break;
}
}
while(choice!= 4);
}

NOTE:
It the choice is not one of the options given i.e. 1.2.3 or 4.the default option is executed.
Control keeps the do….while….loop as long as the choice made is not equal to 4. If 4 is a
chosen, control goes out of the do…while…loop.

For….statement
This is the most commonly used looping statement in C. the general form of the for….statement
is
for(expression 1; expression 2; expression 3)
Statement;
Expression 1 is used to + initialize the control variable i.e, the variable that controls the looping
action.
Expression 2 represents that condition that must be satisfied for the loop to continue execution.
Expression 3 is used to alter the value of the control variable initially assigned by expression 1.
The general flow chart for the for…..statement
10

Initialization expression (exp1)

Test
expressi
on(exp2
)

Body of loop

Update expression
(expression3)

The body of the loop is executed repeatedly as long as the test expression or expression 2 is true.

/*program on for…statement*/
#include<stdio.h>
void main()
{
int i;
int sum = 0,sum_of_squares = 0;
for(i=2; i<=30;i+=2)
{
sum +=i;
sum_of_squares += i*i;
}
printf(“sum of the first 15positive even numbers = %d\n”,sum);
printf(sum of their squares = %d\n”, sum_of_squares);
}

Note: The break and continue statement


11

The break statement is usually used in loop control and switch statements. It takes control out of
the loop or the switch statement.

The continue statement is used in the loop control statement. While, do….while…and
for….statements. When executed, this statements causes the remaining portion of the loop
structure to be skipped as control goes to the beginning of the loop

………….. ……………
………….. ……………-
………….. ……………
………….. ……………
Break; continue;
………….. ……………
………….. ……………
………….. ……………
………….. ……………

You might also like