CH7. Control Flow
CH7. Control Flow
Objectives: At the end of the semester, the student will be able to:
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.
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
// 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
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
.......
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
pseudocode: Do While
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.
BEGIN MAINPROGRAM
procedure1
procedure2
..........
END MAINPROGRAM