COE 281
FLOW OF CONTROL
DR. (MRS) T-S.M.A. ADJAIDOO
TODAY'S LESSON
Decision Iterative Rule of Nested
Statements Statements Braces Statements
COE 281: Programming and Problem-Solving 2
LEARNING OUTCOMES
•Learn how to
1 implement flow
of control in C
COE 281: Programming and Problem-Solving 3
FLOW OF CONTROL
Most programs (like many humans) decide what to do in response to
changing circumstances.
For example selecting a particular option/choice out of a number of
options presented or performing an activity a number of times depending
on the prevailing condition.
COE 281: Programming and Problem-Solving 4
FLOW OF CONTROL
Program statements that help in making a choice or a repetitive action are
called control statements.
There are two major categories of control statements: decisions and
loops.
COE 281: Programming and Problem-Solving 5
DECISION STATEMENTS
Decision statements are control statements that enable the program
select an option from a number of options presented.
This selection is made based on a prevailing condition which should be
true as at the time of selection.
This is very similar to decisions that humans take at certain times. Their
final decision are guided by conditions.
COE 281: Programming and Problem-Solving 6
DECISION STATEMENTS
For example one would decide to eat when hungry and decide not to do so
when not hungry.
Or probably selecting a particular meal to eat depending on the time of
the day.
COE 281: Programming and Problem-Solving 7
DECISION STATEMENTS
There are two kinds of decision statements that are used in programming
with C.
They are:
1. if else statements
2. switch case statements
COE 281: Programming and Problem-Solving 8
DECISION STATEMENTS:
IF ELSE STATEMENTS
The general syntax for the if else statement is:
if (condition)
{
statements; // code to execute if condition is true
}
else
{
statements; // code to execute if condition is false
}
COE 281: Programming and Problem-Solving 9
DECISION STATEMENTS:
IF ELSE STATEMENTS
Bearing in mind that C is a high level language, the if else syntax can be
easily read as:
if the condition is true execute the statements under the if
if the condition is false execute the statements under the else.
COE 281: Programming and Problem-Solving 10
DECISION STATEMENTS: IF ELSE
STATEMENTS
Lets take an example:
int x = 20;
if (x % 2 == 0)
{
printf(“The value is even \n”);
}
else
{
printf(“The value is odd \n”);
}
COE 281: Programming and Problem-Solving 11
DECISION STATEMENTS:
IF ELSE STATEMENTS
The code in the previous slide can be easily read in plain English as:
if x modulus 2 is equivalent to zero print out “The value is even” else print out
“The value is odd”.
This is a simple demonstration of selecting one out of two options
depending on the condition.
COE 281: Programming and Problem-Solving 12
DECISION STATEMENTS:
IF ELSE STATEMENTS
The previous program could have been written to accept a user’s input
rather than having the programmer stating statically that the value of x is
20.
This is demonstrated in the next slide
COE 281: Programming and Problem-Solving 13
DECISION STATEMENTS:
IF ELSE STATEMENTS
int x;
printf(“Please enter an integer value \n”);
scanf(“%d”, &x);
if (x%2 == 0)
{
printf(“%d is even \n”, x);
}
else
{
printf(“%d is odd \n”, x);
} COE 281: Programming and Problem-Solving 14
DECISION STATEMENTS:
IF ELSE STATEMENTS
The previous programs present only two options; what to do when the
statement is true and what to do when the statement is false.
However, there are situations when there are more than two options.
In such situations there is the introduction of an else if in addition with a
condition as shown in the next slide
COE 281: Programming and Problem-Solving 15
DECISION STATEMENTS:
IF ELSE STATEMENTS
if (conditionA)
{
statements; // code to execute if conditionA is true
}
else if (conditionB)
{
statements; // code to execute if conditionB is true
}
else
{
statements; // code to execute if all conditions above are false
} COE 281: Programming and Problem-Solving 16
DECISION STATEMENTS:
IF ELSE STATEMENTS
From the previous slide, it is obvious that this syntax presents the compiler
with 3 options and only one would be selected depending on which
condition is true.
Since C executes procedurally, starting from the top, the very first
condition that the compiler sees to be true would be selected its code
executed.
Even if there are two true conditions.
COE 281: Programming and Problem-Solving 17
DECISION STATEMENTS:
IF ELSE STATEMENTS
int numb;
printf(“Please enter an integer value \n”);
scanf(“%d”, &numb);
if (numb < 0)
{ printf(“%d is negative \n”, numb); }
else if (numb > 0)
{ printf(“%d is positive \n”, numb); }
else
{ printf(“%d is zero \n”, numb); }
COE 281: Programming and Problem-Solving 18
DECISION STATEMENTS:
IF ELSE STATEMENTS
From the example above, we realize that the program tells the user whether the value
he/she enters is positive, negative or zero (3 options).
So to increase the number of options one only needs to include the following as many
times as he/she wants:
else if (condition)
{
statements;
}
COE 281: Programming and Problem-Solving 19
DECISION STATEMENTS:
IF ELSE STATEMENTS
From the examples above, it is obvious that the else statement (the part at
the far bottom) is the default statement. Hence in case all the above
conditions are false the else statement is executed.
However, it is important to note that the else statement is not mandatory
and as such may not always appear when a programmer writes an if else
statement.
COE 281: Programming and Problem-Solving 20
DECISION STATEMENTS:
SWITCH CASE STATEMENTS
Another decision statement in C is the switch case statement.
It is very similar to the if else statement. It could be used in places where
there are many if else statements as in so many multiple if options
However, in the switch case, the exact values that match an option are
used in making a decision.
The following slide shows its general syntax.
COE 281: Programming and Problem-Solving 21
DECISION STATEMENTS:
SWITCH CASE STATEMENTS
switch (expression)
{ case I: statements;
break;
case II: statements;
break;
.
.
case N: statements;
break;
default: statements;
break;
} COE 281: Programming and Problem-Solving 22
DECISION STATEMENTS:
SWITCH CASE STATEMENTS
In the switch, the statements under the case that matches the expression
are executed.
The matched case determines where we start executing from and the
break tells us where we end.
COE 281: Programming and Problem-Solving 23
DECISION STATEMENTS:
SWITCH CASE STATEMENTS
The break means “stop and exit”, it is synonymous to the car’s brake
which causes the car to stop.
So without the break we would keep executing all the statements
downwards under the matched case until we meet a break or come to the
end of the switch.
In cases where none of the cases match the expression, the statements
under the default are executed.
COE 281: Programming and Problem-Solving 24
DECISION STATEMENTS:
SWITCH CASE STATEMENTS
char choice;
printf(“Are you a Ghanaian \? \n”);
printf (“Enter y for yes or n for no \n”);
scanf(“%c”, &choice);
switch(choice)
{
case ‘y’: printf (“Ghanaians are hospitable people \n”);
break;
case ‘n’: printf (“I don’t know much about your country \n”);
break;
default : printf (“You did not follow the instruction \n”);
} COE 281: Programming and Problem-Solving 25
DECISION STATEMENTS:
SWITCH CASE STATEMENTS
The code in the previous slide aims at finding out if the user is a Ghanaian.
The user is asked to enter y for yes and n for no.
If the user’s answer is y, case y would execute and display just “Ghanaians
are hospitable people” and then stop and exit the switch (because of the
break that follows the printf statement)
COE 281: Programming and Problem-Solving 26
DECISION STATEMENTS:
SWITCH CASE STATEMENTS
However, if the user’s answer is n, case n would execute and display just “I
don’t know much about your country” and then stop and exit the switch
(because of the break that follows the printf statement).
Also if the user’s answer is neither y nor n the default case would be
executed
COE 281: Programming and Problem-Solving 27
LOOP STATEMENTS
Loop statements enable certain programming operations or sections of
code to be executed repeatedly as long as a prevailing condition exists or is
true.
Each loop/cycle of execution is seen as an iteration.
COE 281: Programming and Problem-Solving 28
LOOP STATEMENTS
There are three kinds of loop statements that are used in programming
with C.
They are:
1. while loop statements
2. do while loop statements and
3. for loop statements
COE 281: Programming and Problem-Solving 29
LOOP STATEMENTS:
WHILE LOOP
The while loop is used to iterate (loop) some specific set of programming
instructions a number of times based on a specified condition.
The code would keep executing repeatedly until the condition stated
becomes false.
COE 281: Programming and Problem-Solving 30
LOOP STATEMENTS:
WHILE LOOP
The general syntax for the while loop is:
while(condition)
{
statements;
//statements to be executed as long as condition is true
}
COE 281: Programming and Problem-Solving 31
LOOP STATEMENTS:
WHILE LOOP
Bearing in mind that C is a high level language, the while loop syntax can
be easily read as:
while the condition is true execute the statements under the while.
COE 281: Programming and Problem-Solving 32
LOOP STATEMENTS:
WHILE LOOP
Lets take an example:
int q = 5;
while(q<8)
{
printf(“%d\t”, q);
q++; // increase the value of q by 1
}
COE 281: Programming and Problem-Solving 33
LOOP STATEMENTS:
WHILE LOOP
From the code on the previous slide, initially q has a value of 5 and that
makes the condition true (i.e. q<8) so the while loop executes the
statement within it.
The statements within the loop print out the current value of q which is 5
and then after increments the value of q by 1(i.e. q ++) therefore q would
now have a new value of 6 in computer memory.
COE 281: Programming and Problem-Solving 34
LOOP STATEMENTS:
WHILE LOOP
▪ Since it is a loop we would go back and check the condition if it is still
true.
▪ If true we keep executing the statements until the condition turns false.
So the result of the above code is
5 6 7
COE 281: Programming and Problem-Solving 35
LOOP STATEMENTS:
WHILE LOOP
Lets take another example:
int b = 10;
while(b>=7)
{
printf(“%d\t”, b);
b--; // decrease the value of b by 1
}
COE 281: Programming and Problem-Solving 36
LOOP STATEMENTS:
WHILE LOOP
This code is very similar to the previously explained code.
If true we keep executing the statements until the condition turns false,
the output of the code would be
10 9 8 7
COE 281: Programming and Problem-Solving 37
LOOP STATEMENTS:
WHILE LOOP
Note that the position of the statements is very important.
For the next example we switch the statements around. Let’s see the
output generated
COE 281: Programming and Problem-Solving 38
LOOP STATEMENTS:
WHILE LOOP
Lets take another example:
int b = 10;
while(b>=7)
{
b--; // decrease the value of b by 1
printf(“%d\t”, b);
}
COE 281: Programming and Problem-Solving 39
LOOP STATEMENTS:
WHILE LOOP
By virtue of the position of the statements, despite the similarity to the
previously executed code, this code would result in the following output
9 8 7 6
COE 281: Programming and Problem-Solving 40
LOOP STATEMENTS:
DO WHILE LOOP
The do while loop is very much similar to the while loop
However, in the do while loop the statements are executed first before the
condition is checked.
As such, in the do while loop, we are always guaranteed of at least one
iteration.
COE 281: Programming and Problem-Solving 41
LOOP STATEMENTS:
DO WHILE LOOP
The general syntax for the do while loop is:
do
{
statements;
//statements to be executed
}
while(condition); // take note there is a semicolon
COE 281: Programming and Problem-Solving 42
LOOP STATEMENTS:
DO WHILE LOOP
So in the very first instance whether the condition is true or false we go
ahead and do the statements before checking the condition.
And we only continue to do the statements again if the condition is true.
COE 281: Programming and Problem-Solving 43
LOOP STATEMENTS:
DO WHILE LOOP
Lets take an example:
int m = 1;
do
{
printf(“%d\t”, m);
m++;
}
while(m<=5);
COE 281: Programming and Problem-Solving 44
LOOP STATEMENTS:
DO WHILE LOOP
From the previous slide, the code start by first initializing the value of m to
1.
We then go to the do while statement to execute the body of statements
first before checking the condition.
After the first execution we check the condition and keep executing until
the condition becomes false.
So the output of the code would be 1 2 3 4 5
COE 281: Programming and Problem-Solving 45
LOOP STATEMENTS:
DO WHILE LOOP
Lets take another example:
int m=1;
do
{
printf(“%d\t”, m);
m++;
}
while(m>=5);
COE 281: Programming and Problem-Solving 46
LOOP STATEMENTS:
DO WHILE LOOP
From the previous slide, it is obvious that the condition is false from the
beginning.
However, since in the do while we ought to execute the body of
statements before we check the condition the output of the code would be
1
COE 281: Programming and Problem-Solving 47
LOOP STATEMENTS:
FOR LOOP
One of the most used loops in C is the for loop.
It is often used because it is easy to specify how many times the
statements should be looped.
It is also the preferred choice when iterating through arrays.
COE 281: Programming and Problem-Solving 48
LOOP STATEMENTS:
FOR LOOP
The general syntax for the for loop is:
for(initialization; condition; increment/decrement)
{
statements;
}
COE 281: Programming and Problem-Solving 49
LOOP STATEMENTS:
FOR LOOP
INITIALIZATION
The general algorithm
for executing the for
loop is shown below
CONDITION
INCREMENT/
STATEMENTS
DECREMENT
COE 281: Programming and Problem-Solving 50
LOOP STATEMENTS:
FOR LOOP
From the algorithm, it is clear that the initialization is only done once.
Also after the condition is evaluated to be true, the statements are
executed and after the increment/decrement is done (not the other way
round!).
The for loop would keep iterating as long as the condition is true
COE 281: Programming and Problem-Solving 51
LOOP STATEMENTS:
FOR LOOP
Lets take an example:
int a;
for(a=0; a<4; a++)
{
printf(“%d\t”, a);
}
COE 281: Programming and Problem-Solving 52
LOOP STATEMENTS:
FOR LOOP
Going by the earlier explained algorithm, the for loop for the example on
the previous slide would have the following output:
0 1 2 3
COE 281: Programming and Problem-Solving 53
LOOP STATEMENTS:
FOR LOOP
Lets take another example:
int c = 0;
for(c=1; c<10; c*=2)
{
printf(“%d\t”, c);
}
COE 281: Programming and Problem-Solving 54
LOOP STATEMENTS:
FOR LOOP
Once again by the earlier explained algorithm, the for loop for the example
on the previous slide would have the following output
1 2 4 8
COE 281: Programming and Problem-Solving 55
RULE OF THE BRACES {}
In C, there is a rule for using braces (curly brackets) which state that all
programming constructs or elements which usually have the body of their
statements encapsulated/compounded by braces, can choose to ignore
writing the braces when their body of statements is made up of just one
statement.
COE 281: Programming and Problem-Solving 56
RULE OF THE BRACES {}
In other words, constructs and elements such as decision statements,
loops and functions can ignore writing/showing braces when they are
made up of only one statement.
COE 281: Programming and Problem-Solving 57
RULE OF THE BRACES {}
The statement they contain may be an ordinary expression statement
(statement ending with a semicolon) or even a compound statement (such
as flow of control statements and functions).
As long as there is only one statement, the braces can be ignored
COE 281: Programming and Problem-Solving 58
RULE OF THE BRACES {}
For example: Can be written as:
for(int c=1; c<10; c*=2) for(int c=1; c<10; c*=2)
{ printf(“%d\t”, c);
printf(“%d\t”, c);
}
COE 281: Programming and Problem-Solving 59
RULE OF THE BRACES {}
In the example above, when such a for statement is written without
braces, the compiler would interpret that such a for loop has only one
statement in it.
As such, the first statement that follows it would be considered as a part of
it.
COE 281: Programming and Problem-Solving 60
RULE OF THE BRACES {}
Another example: Can be written as:
for(int c=1; c<10; c*=2) for(int c=1; c<10; c*=2)
{ printf(“%d\t”, c);
printf(“%d\t”, c); printf(“\nDone” );
}
printf(“\nDone” );
COE 281: Programming and Problem-Solving 61
RULE OF THE BRACES {}
▪ In the second example above, based on the understanding of this rule, it
is obvious that the statement printf(“\nDone” ); is not a part of the for
loop and as such would not be looped when the condition is satisfied.
▪ We would apply this rule again when we see how compound statements
are nested
COE 281: Programming and Problem-Solving 62
NESTED STATEMENTS
A nested statement is simply a compound statement in another
compound statement.
As stated earlier, compound statements are statements which are usually
followed by braces (eg. Functions, loops and decision statements)
C PROGRAMMING - NESTED STATEMENTS 63
NESTED STATEMENTS
So whenever we have a compound statement in another compound
statement, we describe the inner statement as a nested statement.
C PROGRAMMING - NESTED STATEMENTS 64
NESTED STATEMENTS - EXAMPLE
int x=10, y=0;
while(x<15)
{
if(x%2 == 0)
{ y +=x; }
else
{ y--; }
printf(“%d\t”, y);
x++;
}
C PROGRAMMING - NESTED STATEMENTS 65
NESTED STATEMENTS
In the example, we can see that an if-else compound statement has been
placed inside a while compound statement.
As such the if-else statement can be described as a nested statement.
The if-else statement can only be executed when the while condition is
true.
The example above would result in
10 9 21 20 34
C PROGRAMMING - NESTED STATEMENTS 66
NESTED STATEMENTS
From the previous example it is obvious that the if and else portions of the
code have only one statement as such the rule of the braces can be once
again applied to ignore the braces for them.
This is depicted in the next slide.
C PROGRAMMING - NESTED STATEMENTS 67
NESTED STATEMENTS - EXAMPLE
int x=10, y=0;
while(x<15)
{
if(x%2 == 0)
y +=x;
else
y--;
printf(“%d\t”, y);
x++;
}
C PROGRAMMING - NESTED STATEMENTS 68
NESTED STATEMENTS
Juxtapose the two and see if you can reckon the application of the rule of
braces.
C PROGRAMMING - NESTED STATEMENTS 69
THE END
ANY QUESTION S ?
Contact: tsadjaidoo@[Link]
Office: Caesar Building, Room 413
COE 281: Programming and Problem-Solving 70