Tags: #◇Computer Science #◇CSADPRG #◇Conceptual #◇Statement Level Control Structures
◇Overview
Control statements are fundamentally necessary because simple variable assignment is not enough to fully automate computation. They allow a program to choose different execution paths (conditionals) or repeat statements (loops). Together, a control statement and the statements it governs form a control structure. The design and inclusion of these structures in programming languages are an ongoing balance between writability (which favors more specialized structures) and readability.
◇Key Definitions
- Control Structure: A control statement and the collection of statements whose execution it controls.
- Selection Statement: A control structure that provides a means of choosing between two or more execution paths (e.g., Two-way, N-way or Multiple selection).
- Iterative Statement: A control structure that causes a collection of statements to be executed zero, one, or more times. Often simply referred to as loops.
- Guarded Command: A control structure where a selectable segment can be considered independently of other segments, introducing non-determinism when multiple conditions are met.
◇Theoretical Framework / Concepts
Selection Statements
Selection statements provide the capability to choose between alternate paths of execution.
- Two-way Selection: Usually implemented as
if...then...elseconstructs.- Design issues: What is the form and type of the control expression (Boolean vs arithmetic)? How are
thenandelseclauses specified (single vs compound statements)? How should nested selectors be evaluated? (e.g., the dangling else problem, which is typically addressed by using block braces or specific end markers likeendorfi).
- Design issues: What is the form and type of the control expression (Boolean vs arithmetic)? How are
- Multiple-selection Statements: Allow selecting one path from any number of statements or statement groups. Often implemented using
switchorcasestatements, or via nested two-way selectors.- Design issues: What is the form and type of the control expression? How are selectable segments specified? Is flow restricted to a single segment (e.g., requiring a
breakin C, vs automatic exit in C#)? How are unrepresented values handled (e.g.,defaultcases)?
- Design issues: What is the form and type of the control expression? How are selectable segments specified? Is flow restricted to a single segment (e.g., requiring a
Iterative Statements
Loops are used to automate repetitive tasks that would otherwise be too cumbersome or impossible to write sequentially.
- Iteration Control Mechanisms:
- Logical: Repetition control is based on a Boolean expression (e.g.,
whileloops). - Counting: Controlled by a loop variable, terminal value, and a step size specification (e.g.,
forloops).
- Logical: Repetition control is based on a Boolean expression (e.g.,
- Location of Control Mechanism:
- Pretest: The loop completion condition is evaluated before the body is executed.
- Posttest: The condition is evaluated after the body is executed (e.g.,
do...while), guaranteeing at least one execution. - User-located: Control mechanisms can be placed arbitrarily within the loop body using restricted branches (e.g.,
breakorcontinue).
- Iteration based on Data Structures: Iteration is controlled by the number of elements in a data structure, often using an iterator function that traverses elements until none remain.
- Iteration in Functional Languages: Pure functional languages do not have variables, so iteration is instead implemented using recursion.
Unconditional Branching
The goto statement transfers control to a specified place in the program. While theoretically powerful (all other control structures can be built with goto and a selector), it is considered dangerous because unrestricted use makes programs extremely difficult to read. However, restricted forms of unconditional branching (like break, continue, or labeled exits in Java) are legitimate and widely used.
Guarded Commands
Proposed by Edsger Dijkstra (1975) in the Guarded Command Language (GCL), these commands evaluate all boolean expressions (guards).
- If more than one guard is true, a statement is chosen non-deterministically.
- If none of the guards are true, it raises a runtime exception.
This concept can be applied to both selection (if) and iteration (do), simplifying reasoning about program correctness.
◇Examples
Selection Statements:
// Two-way Selection with block statements in C
if (count == 0) {
result = 0;
} else {
result = 1;
}
# End marker in Ruby
if sum == 0
result = 0
end
Multiple Selection:
# Multiple selection via nested selectors
if count < 10:
bag1 = True
elif count < 100:
bag2 = True
;; Multiple selection in Scheme
(cond
((> x y) "X is greater than Y")
((< x y) "Y is greater than X")
(ELSE "X and Y are equal")
)
Iterative Statements:
// Counting (Pretest)
for (int i = 0; i < 5; i++) {
printf("%d", i);
}
// Logical (Posttest)
do {
printf("%d", i);
i++;
} while (i < 5);
# Iteration based on a data structure
mytuple = ("apple", "banana", "cherry")
for x in mytuple:
print(x)
Guarded Commands:
// Guarded Selection
if x >= y -> max := x
[] y >= x -> max := y
fi
// Guarded Iteration
do q1 > q2 -> temp := q1; q1 := q2; q2 := temp;
[] q2 > q3 -> temp := q2; q2 := q3; q3 := temp;
od
◇References & Navigation
- Previous Topic: ◇Expressions and Assignment Statements
- Next Topic: ◇Subprograms
- Related Notes: ◇Expressions and Assignment Statements
ACTIVE RECALL VERIFICATION
Select an answer to verify conceptual comprehension and reveal the technical rationale.
What do a control statement and the statements it governs form together?
Which statement provides a means of choosing between two or more execution paths?
What design issue is typically addressed by using block braces or specific end markers like end or fi in two-way selection?
In multiple-selection statements, how does C differ from C# regarding flow restriction?
Which iteration control mechanism evaluates the loop completion condition after the body is executed?
How is iteration implemented in pure functional languages?
Which control structure was proposed by Edsger Dijkstra in 1975?
What happens in a Guarded Command if none of the guards are true?
Why is the unrestricted goto statement generally considered dangerous?
Which iteration mechanism is typically controlled by traversing elements until none remain?