pl12ch8
pl12ch8
Statement-Level
Control Structures
ISBN 0-321-49362-1
Chapter 8 Topics
• Introduction
• Selection Statements
• Iterative Statements
• Unconditional Branching
• Guarded Commands
• Conclusions
• 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?
• 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
• Python
if sum == 0 :
if count == 0 :
result = 0
else :
result = 1
1-19
Multiple-Way Selection: Examples
• C#
– Differs from C in that it has a static semantics
rule that disallows the implicit execution of
more than one segment
• Approaches:
– Multiple conditional branches
– Store case values in a table and use a linear
search of the table
– When there are more than ten cases, a hash
table of case values can be used
– If the number of cases is small and more than
half of the whole range of case values are
represented, an array whose indices are the case
values and whose values are the case labels can
be used
• 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
1-30
Counter-Controlled Loops: Examples
– At loop termination, the loop variable has the last value that was
assigned to it
• F#
– Because counters require variables, and functional
languages do not have variables, counter-controlled
loops must be simulated with recursive functions
let rec forLoop loopBody reps =
if reps <= 0 then ()
else
loopBody()
forLoop loopBody, (reps – 1)
- This defines the recursive function forLoop with the
parameters loopBody (a function that defines the
loop’s body) and the number of repetitions
- () means do nothing and return nothing
• F#
– As with counter-controlled loops,
logically-controlled loops can be simulated with
recursive functions
let rec whileLoop test body =
if test() then
body()
whileLoop test body
else ()
1-41
Iteration Based on Data Structures
• PHP
- currentpoints at one element of the array
- next moves current to the next element
- reset moves current to the first element
1-44
Iteration Based on Data Structures (continued)
def traverse(self):
yield nod
• 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)
• Basic Idea: if the order of evaluation is not
important, the program should not specify
one
• 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
• Form
do <Boolean> -> <statement>
[] <Boolean> -> <statement>
...
[] <Boolean> -> <statement>
od
• Semantics: for each iteration
– Evaluate all Boolean expressions
– If more than one are true, choose one
non-deterministically; then start loop again
– If none are true, exit loop