ACHIEVERS UNIVERSITY, OWO
COLLEGE OF NATURAL AND APPLIED SCIENCES
DEPARTMENT OF COMPUTER SCIENCE
COS 201 – COMPUTER PROGRAMMING I – 3 UNITS
LECTURER IN CHARGE - MR. ADEPOJU, S. E.
LECTURE 2
CONTROL STRUCTURE
Introduction
Normally, programs are executed from top to bottom, in the order that they are written. But
sometimes it is required to alter the flow of sequence of instructions in a program. For that, some
programming languages provide some statements that can alter the flow of a sequence of
instructions. The statement which alter and transfer the flow of program from one statement line
to another are called control statements.
One of the most important aspects of programming is controlling which statement will execute
next. Control structures / Control statements enable a programmer to determine the order in
which program statements are executed. These control structures allow you to do two things:
1. skip some statements while executing others, and
2. repeat one or more statements while some condition is true.
Mainly, there are three types of control statements in programming. They are:
i. Sequential control statement
ii. Selection / Branching control Statement
iii. Repetition / Looping / Iteration control Statement
Sequential control statement
By sequential we mean "in sequence," one-after-the-other. Sequential logic is the easiest to
construct and follow. 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. As you can see by the example program in figure 1, the arrows linking the statements
depict the execution flow. If your program included 20 basic commands then it would execute
those 20 statements in order and then quit.
When you are solving a problem as a programmer, you must determine what statements are
needed to create a solution to the problem and the order in which those statements must be
executed. Writing the correct statements is one task. Determining where to place those
statements in your program is equally important. For example, when you want to get and process
data from the user
you have to GET the data before you can use it. Switching the order of these statements would
produce an invalid program.
Figure 1: Sequential control statement
Sequential control is the "default" control in the sense that every statement automatically points
to the next statement in the flowchart diagram. You do not need to do any extra work to make
sequential control happen. However, using sequential control alone will not allow the
development of solutions for most real-world problems. Most real world problems include
"conditions" that determine what should be done next. For example, "If it is after taps, then turn
your lights out," requires a decision to be made based on the time of day. The "condition" (i.e.,
the current time of day) determines whether the action should be executed or not executed. This
is called "selection control" and is introduced next.
Selection / Branching control statement
It is common that you will need to make a decision about some condition of your program's data
to determine whether certain statements should be executed. For example, if you were
calculating the slope of a line using the assignment statement, slope ← dy / dx, then you need to
make sure that the value of dx is not zero (because division by zero is mathematically undefined
and will produce a run-time error). Therefore, the decision you would need to make is, "Is dx
zero?"
A selection-control statement allows you to make "decisions" in your code about the current state
of your program's data and then to take one of two alternative paths to a "next" statement.
Figure 2: Selection control statement
The flowchart in figure 2 illustrates a selection control statement, which is always drawn as a
diamond. All decisions are stated as "yes/no" questions. When a program is executed, if the
answer to a decision is "yes" (or true), then the left branch of control is taken. If the answer is
"no" (or false), then the right branch of control is taken. In the example in figure 2, either
statement 2a or statement 2b will be executed, but never both. Note that there are two possible
executions of this example program:
Table 1
Possibility 1 Possibility 2
Statement 1 Statement 1
Statement 2a Statement 2b
Statement 3 Statement 3
Also note that either of the two paths of a selection control statement could be empty or could
contain several statements. It would be inappropriate for both paths to be empty or for both paths
to have the exact same statements, because then your decision, Yes or No, would have no effect
during program execution (since nothing different would happen based on the decision).
Repetition / Looping / Iteration control statement
A Loop control statement allows you to repeat one or more statements until a specified condition
is met. They are essential tools that allow for the execution of certain operations until the
condition becomes false. This type of control statement is what makes computers so valuable. A
computer can repeatedly execute the same instructions over-and-over again without getting bored
with the repetition.
The number of times that the loop is executed is controlled by the decision expression that is
entered into the diamond symbol. During execution, when the diamond symbol is executed, if
the decision expression evaluates to "yes," then the "yes" branch is taken, which always leads
back to the Loop statement and repetition. The statements to be repeated can be placed above or
below the decision diamond.
Figure 3: Repetition control statement
To understand exactly how a loop statement works, study the flowchart in figure 3 and notice the
following things about this program:
● Let’s assume there exists a statement (called statement 1) before the condition and the
statement after the condition is statement 2.
● The program starts, statement 1 will always be executed at least once because it comes
before the decision statement.
● If the decision expression evaluates to "false", then the loop terminates and control is
passed to the end statement.
● If the decision expression evaluates to "true", then control passes to Statement 2 and
Statement 2 is executed next. Then control is returned back up to the Loop statement
which re-starts the loop.
● Note that Statement 1 is guaranteed to execute at least once. Also note that Statement 2 is
possibly never executed.
There are too many possible executions of this example program to list them all, but a few of the
possibilities are listed in table 2. Make sure you can fill in the fourth column with the correct
pattern.
Table 2
Possibility 1 Possibility 2 Possibility 3 Possibility 4
Statement 1 Statement 1 Statement 1
Decision (“false”) Decision (“true”) Decision (“true”)
End Statement 2 Statement 2
Decision (“false”) Decision (“true”)
End Statement 2
Decision (“false”)
End
It is possible that the "Decision" statement never evaluates to "false". In such a case you have an
"infinite loop" that will never stop repeating. (If this ever happens, you will have to manually
stop your program by selecting the "stop" icon in the tool bar). You should never write
statements that create an infinite loop. Therefore, one (or more) of the statements in the loop
must change one or more of the variables in the "Decision" statement such that it will eventually
evaluate to "false".
Other control statement
All other control statements in programming are from the above listed three types of control
statement. For example:
Cascading selection statement
A single selection-control statement can make a choice between one or two choices. If you need
to make a decision that involves more than two choices, you need to have multiple selection
control statements. For example, if you are assigning a letter grade (A, B, C, D, or F) based on a
numeric score, you need to select between five choices, as shown in figure 4. This is sometimes
referred to as “cascading selection control,” as in water cascading over a series of water falls.
Figure 4: Cascading selection statement
ASSIGNMENT
1. Draw a flowchart that determines whether a number is positive, negative, or zero
2. Draw a flowchart that checks if a person is eligible to vote based on their age.