9. Two Way Selection Statement
• ALGOL 60 2-way selector:
if (Boolean_expr)
then statement
else statement
• Nested if-else
• Special delimiters and selection closure ALGOL
68, FORTRAN 77, Ada use end if
10. Two Way Selection Statement
• 2-Way Selection Statements ALGOL 60's
solution - disallow direct nesting
16. Multiple-Way Selection Statements
Design issues:
• What is the form and type of expression that controls the
selection
• May single statements, sequences of statements, or compound
statements be selected
• Is the entire construct encapsulated in a syntactic structure
• Is execution flow through the structure restricted to include just a
single selectable segment
• How should unrepresented selector expression values be
handled
17. Multiple Selection Statements
Design choices:
• Expression is any ordinal type
(int, boolean, char, enum)
• Segments can be single or compound
• Only one segment can be executed per execution of the
construct
• In Wirth's Pascal, result of an unrepresented control
expression value is undefined (In 1984 ISO Standard, it is a
runtime error)
• Many dialects now have otherwise or else clause
18.
19. Modern multiple selectors
• C, C++, Java switch statement
• FORTRAN arithmetic IF
• Ada case statement
• Pascal case statement
20. Multiple Way Selection -Example
More reliable than C’s switch (once a stmt_sequence execution
is completed, control is passed to the first statement after the
case statement
21. Multiple-Way Selection Using if
• Multiple Selectors can appear as direct extensions to two-way
selectors,
• using else-if clauses, for example in Python:
22. Multiple-Way Selection Using if
• The Python example can be written as a Ruby
case
Far more readable than deeply nested if 's Allows a Boolean gate on every
selectable group
23. Iterative Statements
• The repeated execution of a statement or
compound statement is accomplished either
by iteration or recursion
• General design issues for iteration control
statements:
1. How is iteration controlled?
2. Where is the control mechanism in the loop?
24. 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:
• What are the type and scope of the loop variable?
• What is the value of the loop variable at loop termination?
• 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?
• Should the loop parameters be evaluated only once, or once
for every iteration?
25. Iterative Statements: Examples
Design choices:
• 1. Loop variable must be INTEGER
• 2. Loop variable always has its last value
• 3. The loop variable cannot be changed in the loop, but the
parameters can; because they are evaluated only once, it does
not affect loop control
• 4. Loop parameters are evaluated only once
28. Iterative Statements: Examples
Ada Design choices:
• Type of the loop variable is that of the discrete range;
• its scope is the loop body (it is implicitly declared)
• The loop variable does not exist outside the loop
• The loop variable cannot be changed in the loop, but the
discrete range can; it does not affect loop control
• The discrete range is evaluated just once
29. Iterative Statements: Examples
• 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
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
30. Iterative Statements: Examples
C++ differs from C in two ways:
• The control expression can also be Boolean
• The initial expression can include variable definitions (scope is
from the definition to the end of the loop body)
• Java and C#
-Differs from C++ in that the control expression must be
Boolean
32. Iterative Statements: Logically-Controlled
Loops
• Repetition control is based on a Boolean
expression
Design issues:
• Pretest or posttest?
• Should the logically controlled loop be a
special case of the counting loop statement or
a separate statement?
33. Iterative Statements: Logically-Controlled
Loops: Examples
• C and C++ have both pretest and posttest forms, in which the
control expression can be arithmetic:
• Java is like C and C++, except the control expression must be
Boolean
34. Iterative Statements: Logically-Controlled
Loops: Examples
• Ada has a pretest version, but no posttest
• FORTRAN 95 has neither
• Perl and Ruby have two pretest logical loops,
while and until. Perl also has two posttest
loops
35. Iterative Statements: User-Located Loop
Control Mechanisms
• Sometimes it is convenient for the
programmers to decide a location for loop
control (other than top or bottom of the loop)
• Simple design for single loops (e.g., break)
• Design issues for nested loops
• Should the conditional be part of the exit?
• Should control be transferable out of more
than one loop?
36. Iterative Statements: User-Located Loop
Control Mechanisms break and continue
• C , C++, Python, Ruby, and C# have unconditional unlabeled
exits (break)
• Java and Perl have unconditional labeled exits (break in Java,
last in Perl)
• C, C++, and Python have an unlabeled control statement,
continue, that skips the remainder of the current iteration,
but does not exit the loop
• Java and Perl have labeled versions of continue
37. Iterative Statements: Iteration Based on
Data Structures
• Number of elements of in a data structure control
loop iteration
• Control mechanism is a call to an iterator function
that returns the next element in some chosen order,
if there is one; else loop is terminate
• C's for can be used to build a user-defined iterator:
for (p=root; p==NULL; traverse(p)){
}
39. Unconditional Branching
• Transfers execution control to a specified place in the program
• Represented one of the most heated debates in 1960’s and
1970’s
• Well-known mechanism: goto statement
• Major concern: Readability
• Some languages do not support goto statement (e.g., Java)
• C# offers goto statement (can be used in switch statements)
• Loop exit statements are restricted and somewhat
camouflaged goto’s
40. 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
41. Guarded Commands
if <Boolean expression> -> <statement>
[ ] <Boolean expression> -> <statement>
[ ] . . .
[] <Boolean expression> -> <statement>
fi
• The closing reserved word, fi, is the opening reserved word spelled
backward. This form of closing reserved word is taken from ALGOL 68.
• The small blocks, called fatbars, are used to separate the guarded clauses
and allow the clauses to be statement sequences.
• Each line in the selection statement, consisting of a Boolean expression (a
guard) and a statement or statement sequence, is called a guarded
command.
43. Guarded Commands
• If more than one expression is true, one of the corresponding
statements can be non-deterministically chosen for execution.
• If i = 0 and j > i, this statement chooses non-deterministically
between the first and third assignment statements.
• If i is equal to j and is not zero, a runtime error occurs
because none of the conditions is true
44. Guarded Commands
• This computes the desired result without over-
specifying the solution.
• In particular, if x and y are equal, it does not matter
which we assign to max. This is a form of abstraction
provided by the nondeterministic semantics of the
statement
46. Guarded Command
The loop structure proposed by Dijkstra has the form
The semantics of this statement is that all Boolean expressions are
evaluated on each iteration. If more than one is true, one of the
associated statements is non-deterministically (perhaps randomly)
chosen for execution, after which the expressions are again evaluated.
When all expressions are simultaneously false, the loop terminates.
47. Guarded Commands
• Now, consider the following code, which uses guarded commands to solve
the same problem but in a more concise and elegant way.
48. Guarded Commands
• Dijkstra’s guarded command control statements are interesting, in part
because they illustrate how the syntax and semantics of statements can
have an impact on program verification and vice versa
49. Guarded Commands: Rationale
• Connection between control statements and
program verification is intimate
• Verification is impossible with goto statements
• Verification is possible with only selection and
logical pretest loops
• Verification is relatively simple with only
guarded commands