Learning Outocme D1
Learning Outocme D1
(PPA115D/TRO115D)
Compiled by
V. Booi
and
V. Memani
Learning outcome: D.1. Introduction to control structures
The purpose of this chapter is to introduce the student to control structures. We will
define control structures, look at the different types, and also talk about conditional
statements.
We make decisions on many things, we decide when to sleep, when and what to eat,
how long to watch TV, and so on. So life is about making decisions and repetition, and
so is programming.
In programming we write programs that make decisions and repeat the execution of
certain instructions. In order to do this we use control structures. Control structures
give our programs the ability to make decisions and repeat tasks. There are two kinds
of control structures used in programming, and they are:
Selection control structures; and
Iteration control structures.
We will discuss in detail each of the control structures in the next sections.
1.2 Selection control structures
Selection control structures are programming structures that are used to make
decisions or choices. They allow programs to make decisions as to which instructions
must be executed over others. The selection is based on the evaluation outcome of a
condition. A condition is tested and the next move is dependent on whether the
condition evaluates to a true or false. We mainly use relational operators to construct
conditions. If there are more than two conditions to be constructed, we use Boolean
operators to combine the conditions.
Selection control structures come in various types. The following statements are
examples of selection control structures:
if statement;
if…else statement;
if …else if …else statement;
Nested if statements; and the
switch statement.
In chapter the next chapter we discuss in detail each of the statements. Let us have a
look at the generic representation of selection control structures using the flowchart.
We have three types of iteration control structures, and they are the:
for loop;
while loop; and
do…while loop.
These specific types of iteration control structures will be studied in detail in chapter
eight. Let us look at the generic representation of iteration control structures using a
flowchart.
We can see from Figure 2 that the loop will continue working as long the condition
tests TRUE. Should the condition test FALSE, the loop will terminate or come to an
end. The statements that we want to execute repeatedly (block A) must be kept inside
the loop, that is, they must become body statements of the loop. Those we want to
execute after the loop has terminated (block B), we keep outside of the loop.
1.4 Conditions
For selection and iteration control structures we use conditions to determine what must
happen next. If the condition evaluates to TRUE we do something different compared
to when the outcome is FALSE.
There’s also the unary Boolean operator, ! (NOT). It acts on a single operand. As said
in chapter 3, it returns the opposite of the operand.
1.5 Conclusion
In the next two chapters we are going to look at the specific types of control structures.
Thank you very much for having taken time to go through this chapter. Enjoy the rest
of the day and God bless you.