Flow of Control
Flow of Control
FLOW OF CONTROL
DECISIONS
LOOPS
NESTED STATEMENTS
2
INTENDED LEARNING OUTCOMES (ILOs)
To To be able
To
understand to mentally
understand
decision analyze
loop
statements nested
statements
statements
3
FLOW OF CONTROL
4
FLOW OF CONTROL
5
FLOW OF CONTROL
6
1
DECISION STATEMENTS
7
DECISION STATEMENTS
8
DECISION STATEMENTS
▪ For example one would decide to eat when hungry and decide not
to do so when not hungry.
9
DECISION STATEMENTS
10
DECISION STATEMENTS: if else Statements
11
DECISION STATEMENTS: if else Statements
12
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.
14
DECISION STATEMENTS: if else Statements
15
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);
}
16
DECISION STATEMENTS: if else Statements
17
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
}
18
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.
19
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);
} 20
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;
}
21
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.
22
DECISION STATEMENTS: switch case Statements
switch (expression)
{
case I: statements;
break;
case II: statements;
break;
.
.
.
case N: statements;
break;
default: statements;
break;
}
24
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.
25
DECISION STATEMENTS: switch case Statements
26
DECISION STATEMENTS: switch case Statements
int choice;
printf(“Are you a Ghanaian \? \n”);
printf (“Enter 10 for yes of 20 for no \n”);
scanf(“%d”, &choice);
switch(choice)
{
case 10: printf (“Ghanaians are hospitable people \n”);
break;
case 20: printf (“I don’t know much about your country \n”);
break;
default : printf (“You did not follow the instruction \n”);
}
27
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 10 for yes and 20 for no.
▪ If the user’s answer is 10, case 10 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)
28
DECISION STATEMENTS: switch case Statements
29
2
LOOP STATEMENTS
30
LOOP STATEMENTS
31
LOOP STATEMENTS
32
LOOP STATEMENTS – while loop
33
LOOP STATEMENTS – while loop
34
LOOP STATEMENTS – while loop
35
LOOP STATEMENTS – while loop
36
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.
37
LOOP STATEMENTS – while loop
38
LOOP STATEMENTS – while loop
39
LOOP STATEMENTS – while loop
10 9 8 7
40
LOOP STATEMENTS – while loop
41
LOOP STATEMENTS – while loop
42
LOOP STATEMENTS – while loop
43
LOOP STATEMENTS – do while loop
44
LOOP STATEMENTS – do while loop
45
LOOP STATEMENTS – do while loop
46
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
48
LOOP STATEMENTS – do while loop
50
LOOP STATEMENTS – for loop
51
LOOP STATEMENTS – for loop
52
LOOP STATEMENTS – for loop
▪ The general algorithm for executing the for loop is shown below
53
LOOP STATEMENTS – for loop
54
LOOP STATEMENTS – for loop
55
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
56
LOOP STATEMENTS – for loop
57
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
58
3
RULE OF THE BRACES {}
59
RULE OF THE BRACES {}
60
RULE OF THE BRACES {}
61
RULE OF THE BRACES {}
62
RULE OF THE BRACES {}
63
RULE OF THE BRACES {}
64
RULE OF THE BRACES {}
▪ Another example Can be written as such
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” );
65
RULE OF THE BRACES {}
66
4
NESTED STATEMENTS
67
NESTED STATEMENTS
68
NESTED STATEMENTS
69
NESTED STATEMENTS - Example
72
NESTED STATEMENTS - Example
▪ Juxtapose the two and see if you can reckon the application of the
rule of braces.
74
THANKS!
Any questions?
You can find me at
[email protected] & [email protected]
75