6 Control Structures
6 Control Structures
Introduction
A control structure is simply a pattern for controlling the flow of a
program module.
Essentially you place each statement in the order that you want them
to be executed and the program executes them in sequence from the
Start statement to the End statement.
if statement
1. Simple if statement
2. If …. else statement
E.g.
Example
Input voters’
name and age
If YES
age> Vote
=18
NO
Not
allowed
to vote
STOP
Program code
# include <stdio.h>
int main()
{ char vname[10];
int age;
printf (“Enter voters’ name”);
scanf (“%s”,& vname);
printf (“Enter age”);
scanf (“%d”,& age);
if (age>=18) printf (“Vote”);
else printf (“Not allowed to vote”);
}
The if …… else Statement
else false-statement(s);
Example
Arithmetic operators
+ , - , *, /
if (test-conditions-1)
{
if (test-conditions-2);
statement-1;
else statement - 2; } statement-x ;
}
If the condition-1 is true, condition-2 is tested and if
condition-2 is true then and statement-1 will be evaluated
otherwise statement-2 will be executed if condition-2 is
false.
If (gender==“female)
{if (mean>=60) printf (“Admit student”);
else printf (“don’t admit”);}
Else printf (“Not admissible”);
Example
if (sex is female)
Braces { }- Used to indicate the start and end of the switch statement block.
Case statement- Each case statement specifies a value to compare with the
value specifies in the switch statement.
Default – It is an optional statement included to indicate the message or
statement to be executed if none of the case statements were matched.
Syntax
switch (value)
{
case 1: first alternative statement;
break;
case 2: second alternative statement;
break;
case 3: last alternative statement;
break;
default: default statement;
}
NB: Switch statement can only be used for ordinal
data types such as character and integer data types.
Float data types are not allowed as switch values.
Example
NB: In while loop, the test is done at the start. i.e. test then execute.
The while statement evaluates expression, which
must return a boolean value. If the expression
evaluates to true, the while statement executes the
statement(s) in the while block.
While
expressi No
on
Yes
Statement(s)
counter
Count=0
While No
(count<1
0)
Yes
Display Hallo
Count++
stop
# include <stdio.h>
Int main()
{ int counter;
counter = 1;
while(counter<=8)
{ printf (“%d”, counter);
counter++;
}
}
# include <stdio.h>
Int main()
{ int counter;
counter = 0;
while(counter<=12)
{ printf (“%d”, counter);
counter=counter +2;
}
}
# include <stdio.h>
Int main()
{ int counter;
counter = 10;
while(counter>=1)
{ printf (“%d”,counter);
printf (“\n”);
counter--;
}
}
#include <stdio.h> printf (“Enter English”);
int main() scanf(“%d”,&eng);
{int count; printf (“Enter Kiswahili”);
int mat,eng,kis,tot; float avg; scanf(“%d”,&kis);
char sname[10]; tot=mat+eng+kis;
count=0; avg=tot/3;
while (count<5) Printf (“Total is %d \n”,tot);
{ printf (“Enter student name”); Printf (“Average is %f \n”,avg);
scanf(“%s”,&sname); Count++;
printf (“Enter mathematics”); }
scanf(“%d”,&mat); }
do while
Unlike for and while loops, which test the loop condition at the top of the loop, the
do...while loop in C programming language checks its condition at the bottom of
the loop.
statement(s)
}
# include <stdio.h>
Int main()
{ int counter;
counter = 10;
do { printf (“%d”,counter);
printf (“\n”);
counter=counter-1;
} while(counter>=1);
}