The Kindness
The Kindness
Programming Languages
Lecture 8 – Statement-Level
Control Structures
Selection Statements
• A selection statement provides the means
of choosing between two or more paths of
execution
• Two general categories:
– Two-way selectors
– Multiple-way selectors
Two-Way Selection Statements
• General form:
if control_expression
then clause
else clause
• Design Issues:
– What is the form and type of the control expression?
– How are the then and else clauses specified?
– How should the meaning of nested selectors be
specified?
Nesting Selectors
• Java example
if (sum == 0)
if (count == 0)
result = 0;
else result = 1;
• Which if gets the else?
• Java's static semantics rule: else matches
with the nearest previous if
Nesting Selectors (continued)
• To force an alternative semantics,
compound statements may be used:
if (sum == 0) {
if (count == 0)
result = 0;
}
else result = 1;
• The above solution is used in C, C++, and
C#
Selector Expressions
• In ML, F#, and LISP, the selector is an
expression
• F#
let y =
if x > 0 then x
else 2 * x
– If the if expression returns a value, there must
be an else clause (the expression could
produce output, rather than a value)
Multiple-Way Selection Statements
• Allow the selection of one of any number of
statements or statement groups
• Design Issues:
1. What is the form and type of the control expression?
2. How are the selectable segments specified?
3. Is execution flow through the structure restricted to
include just a single selectable segment?
4. How are case values specified?
5. What is done about unrepresented expression values?
Counter-Controlled Loops
• A counting iterative statement has a loop variable,
and a means of specifying the initial and terminal,
and stepsize values
• Design Issues:
1. What are the type and scope of the loop variable?
2. Should it be legal for the loop variable or loop
parameters to be changed in the loop body, and if so,
does the change affect loop control?
3. Should the loop parameters be evaluated only once,
or once for every iteration?
Counter-Controlled Loops: Examples
• Ada
for var in [reverse] discrete_range loop
...
end loop
• C-based languages
for ([expr_1] ; [expr_2] ; [expr_3])
statement
– The expressions can be whole statements, or even
statement sequences, with the statements separated by
commas
– The value of a multiple-statement expression is the
value of the last statement in the expression
– If the second expression is absent, it is an infinite loop
• Design choices:
– There is no explicit loop variable
– Everything can be changed in the loop
– The first expression is evaluated once, but the
other two are evaluated with each iteration
– It is legal to branch into the body of a for loop
in C
Counter-Controlled Loops: Examples
names.Add("Bob");
names.Add("Carol");
names.Add("Ted");
Guarded Commands
• Designed by Dijkstra
• Purpose: to support a new programming
methodology that supported verification
(correctness) during development
• Basis for two linguistic mechanisms for
concurrent programming (in CSP and Ada)
• Basic Idea: if the order of evaluation is not
important, the program should not specify one
Selection Guarded Command
• Form
if <Boolean expr> -> <statement>
[] <Boolean expr> -> <statement>
...
[] <Boolean expr> -> <statement>
fi
• Semantics: when construct is reached,
– Evaluate all Boolean expressions
– If more than one are true, choose one non-
deterministically
– If none are true, it is a runtime error