Module 7 – Control Flow
Objectives: At the end of the semester, the student will be able to:
1. Discuss the importance of control flow statements
2. Explain how control flow statements are implemented
3. Create a solution with different control flows
In computer programming, control flow or flow of control is the order function calls, instructions,
and statements are executed or evaluated when a program is running. Many programming
languages have what are called control flow statements, which determine what section of code
is run in a program at any time.
Sequencing
Sequencing is the most basic control flow... it refers to doing elaborations, evaluations, or
executions one after another.
The execution of statements and evaluation of expressions is
usually in the order in which they appear in a program text
In a computer program or an algorithm, sequence involves simple steps which are to be
executed one after the other. Each step is executed in the same order in which it appears
In pseudocode this would be
process 1
process 2
process 3
...........
process n
Selection
A run-time condition determines the choice among two or more
statements or expressions
The basic attribute of a selection control structure is to be able to select between two or more
alternate paths. This is described as either two-way selection or multi-way selection. A
question using Boolean concepts usually controls which path is selected. All of the paths from
a selection control structure join back up at the end of the control structure, before moving on
to the next lines of code in a program.
Selection involves testing a condition. Depending on the result of the test the program control
branches from the main path.
A Binary Branch occurs as a result of testing a condition whose answer is either 'Yes' or 'No'
COSC 65 – PROGRAMMING LANGUAGE
In pseudocode a binary branch is created using an
IF THEN ELSE ENDIF Statement as shown below.
IF (condition is true) THEN
// do this
ELSE
// do that
ENDIF
Multiple Branching can also occur. A CASE statement is used to set up this control structure.
In pseudocode a CASE Statement looks like this.
CASEWHERE (value =)
a : process
b : process
c : process
................
OTHERWISE
process
ENDCASE
Iteration
Iteration is the process of Looping. Loop structures can be created in a variety of ways.
The basic attribute of an iteration control structure is to be able to repeat some lines of code.
The visual display of iteration creates a circular loop pattern when flowcharted, thus the word
“loop” is associated with iteration control structures. Iteration can be accomplished with test
before loops, test after loops, and counting loops. A question using Boolean concepts usually
controls how often the loop will execute.
Pre-test Loop
A pre-tested loop is so named because the condition has to be met at the very beginning of
the loop.
The segment of the program will not be executed if the condition is not met.
This construct is also called a guarded loop or a WHILE Loop. The body of the loop is executed
repeatedly while the condition is true. eg
DEPARTMENT OF INFORMATION TECHNOLOGY
2
CVSU-CAVITE CITY
COSC 65 – PROGRAMMING LANGUAGE
WHILE (condition is True)
do this
......
END WHILE
Post-test Loop
A post-test loop executes the body of the loop at least once before the termination condition
can be met. The body of the loop is repeatedly executed until the termination condition is true.
eg
REPEAT
do this
.......
UNTIL (condition is True)
Counted Loop
When you know exactly how many time you want something to repeat you can set up a
structure called a Counted Loop or FOR NEXT Loop. eg
FOR counter = 1 to 10
do this
...........
NEXT counter
Iteration (Repetition) Control Structures
pseudocode: While
count assigned zero
While count < 5
Display "I love computers!"
Increment count
End
pseudocode: Do While
DEPARTMENT OF INFORMATION TECHNOLOGY
3
CVSU-CAVITE CITY
COSC 65 – PROGRAMMING LANGUAGE
count assigned five
Do
Display "Blast off is soon!"
Decrement count
While count > zero
pseudocode: Repeat Until
count assigned five
Repeat
Display "Blast off is soon!"
Decrement count
Until count < one
pseudocode: For
For x starts at 0, x < 5, increment x
Display "Are we having fun?"
End
Modularization
Modularization is the process of breaking a program
solution up into Subprograms.
This refinement makes it easier to work on solution
and easier to follow them.
Generally, you end up with a mainline program
showing subprogram names and several
subprograms which provide more detail.
In pseudocode the subprogram names are underlined
eg
BEGIN MAINPROGRAM
procedure1
procedure2
..........
END MAINPROGRAM
Each subprogram then has its own solution starting
with BEGIN SUBPROGRAM followed by the underlined title used in the main program.
The end of the subprogram is marked by the keywords END SUBPROGRAM
DEPARTMENT OF INFORMATION TECHNOLOGY
4
CVSU-CAVITE CITY