PROGRAMMING LANGUAGES
STATEMENT-LEVEL CONTROL
STRUCTURES
Logically Controlled Loops
Repetition control is based on a Boolean expression rather than a counter
Design Issues:
– Should the control be pretest or posttest?
– Should the logically controlled loop be a special form of a counting loop
or a separate statement?
The C-based programming languages include both pretest and posttest
logically controlled loops that are not special forms of their counter-
controlled iterative statements
The pretest and posttest logical loops have the following forms (while and
do-while):
Statement-Level Control Structures
Logically Controlled Loops
Statement-Level Control Structures
Logically Controlled Loops
Statement-Level Control Structures
Logically Controlled Loops
The only real difference between the do and the while is that the do
always causes the loop body to be executed at least once
Java’s while and do statements are similar to those of C and C++, except
the control expression must be Boolean type, and because Java does not
have a goto, the loop bodies cannot be entered anywhere but at their
beginning
Statement-Level Control Structures
User-Located Loop Control Mechanisms
It is sometimes convenient for a programmer to choose a location for loop
control other than the top or bottom of the loop
Design issues:
– Should the conditional mechanism be an integral part of the exit?
– Should only one control body be exited, or can enclosing loops also be
exited?
C and C++ have unconditional unlabeled exits (break)
Java, Perl, and C# have unconditional labeled exits (break in Java and C#,
last in Perl)
The following is an example of nested loops in C#:
Statement-Level Control Structures
User-Located Loop Control Mechanisms
Statement-Level Control Structures
User-Located Loop Control Mechanisms
C and C++ include an unlabeled control statement, continue, that transfers
control to the control mechanism of the smallest enclosing loop
This is not an exit but rather a way to skip the rest of the loop statements
on the current iteration without terminating the loop structure. Ex:
Statement-Level Control Structures
User-Located Loop Control Mechanisms
A negative value causes the assignment statement to be skipped, and
control is transferred instead to the conditional at the top of the loop
On the other hand, in
Statement-Level Control Structures
User-Located Loop Control Mechanisms
Java, Perl, and C# have statements similar to continue, except they can
include labels that specify which loop is to be continued
The motivation for user-located loop exits is simple: They fulfill a common
need for goto statements through a highly restricted branch statement
The target of a goto can be many places in the program, both above and
below the goto itself
However, the targets of user-located loop exits must be below the exit and
can only follow immediately the end of a compound statement
Statement-Level Control Structures
Iteration Based on Data Structures
A general data-based iteration statement uses a user-defined data
structure and a user-defined function (the iterator) to go through the
structure’s elements
The iterator is called at the beginning of each iteration, and each time it is
called, the iterator return a n element from a particular data structure in
some specific order
C's for can be used to build a user-defined iterator:
Statement-Level Control Structures
Iteration Based on Data Structures
Statement-Level Control Structures
Iteration Based on Data Structures
C#’s foreach statement iterates on the elements of array and other
collections
– C# and F# (and the other .NET languages) have generic library classes,
like Java 5.0 (for arrays, lists, stacks, and queues)
– For example, there are generic collection classes for lists, which are
dynamic length array, stacks, queues, and dictionaries (has table)
– All of these predefined generic collections have built-in iterator that are
used implicitly with the foreach statement
– Furthermore, users can define their own collections and write their
own iterators, implement the IEnumerator interface, which enables the
use of use foreach on these collections
Statement-Level Control Structures
Iteration Based on Data Structures
Statement-Level Control Structures
Unconditional Branching
An unconditional branch statement transfers execution control to a
specified place in the program
The unconditional branch, or goto, is the most powerful statement for
controlling the flow of execution of a program’s statements
However, using the goto carelessly can lead to serious problems
Without restrictions on use, imposed by either language design or
programming standards, goto statements can make programs very difficult
to read, and as a result, highly unreliable and costly to maintain
Statement-Level Control Structures
Unconditional Branching
There problems follow directly from a goto’s ability to force any program
statement to follow any other in execution sequence, regardless of
whether the statement proceeds or follows previously executed statement
in textual order
Java, Python, and Ruby do not have a goto. However, most currently
popular languages include a goto statement
C# uses goto in the switch statement
Statement-Level Control Structures
Guarded Commands
New and quite different forms of selection and loop structures were
suggested by Dijkstra (1975)
His primary motivation was to provide control statements that would
support a new program design methodology that ensured correctness
(verification) during development rather than when verifying or testing
completed programs
Basis for two linguistic mechanisms for concurrent programming in CSP
(Hoare, 1978)
Basic idea: if the order of evaluation is not important, the program should
not specify one
Statement-Level Control Structures
Guarded Commands
Dijkstra’s selection guarded command has the form
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
Statement-Level Control Structures
Guarded Commands
Ex
– 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 run-time error occurs because none of
the condition are true
Statement-Level Control Structures
Guarded Commands
Ex
– 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 provide by the non-deterministic semantics
of the statement
Statement-Level Control Structures
Guarded Commands
The loop structure proposed by Dijkstra has the form
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
Statement-Level Control Structures
Guarded Commands
Ex Consider the following problem:
Given four integer variables, q1, q2, q3, and q4, rearrange the values of the
four so that q1 ≤ q2 ≤ q3 ≤ q
– Without guarded commands, one straightforward solution is to put the
four values into an array, sort the array, and then assign the values from
the array back into the scalar variables q1, q2, q3, and q4. While this
solution is not difficult, it requires a good deal of code, especially if the
sort process must be included.
– Now, uses guarded commands to solve the same problem but in a more
concise and elegant way
Statement-Level Control Structures
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.
Program verification is impossible when goto statements are used
Verification is greatly simplified if
– only selection and logical pretest loops
– only guarded commands
Statement-Level Control Structures
Summary
Statement-Level Control Structures
Summary
Statement-Level Control Structures
Thank you.