Programming
Programming
FLOW OF CONTROL
•Learn how to
1 implement flow
of control in C
COE 281: Programming and Problem-Solving 3
FLOW OF CONTROL
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.
There are two kinds of decision statements that are used in programming
with C.
They are:
1. if else statements
2. switch case 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.
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.
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
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
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.
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;
}
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.
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.
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.
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)
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
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
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.
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.
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.
5 6 7
10 9 8 7
9 8 7 6
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
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
CONDITION
INCREMENT/
STATEMENTS
DECREMENT
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
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
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
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.
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.
Juxtapose the two and see if you can reckon the application of the rule of
braces.
Contact: [email protected]
Office: Caesar Building, Room 413