Levels of Control Flow
Levels of Control Flow
Evolution:
1
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
in the form of begin...end
7
Compound statements - introduced by ALGOL 60
Selection Statements
Design Issues:
Single-Way Examples
2
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
7
FORTRAN example:
ALGOL 60 if:
if (boolean_expr) then
begin
...
end
ALGOL 60 if:
if (boolean_expr)
then statement (the then clause)
else statement (the else clause)
3
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
Nested Selectors 7
e.g. (Pascal) if ... then
if ... then
...
else ...
4
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
7
FORTRAN 77, Ada, Modula-2 solution - closing
special words
e.g. (Ada)
5
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
7
Multiple Selection Constructs
Design Issues:
Bad aspects:
- Not encapsulated (selectable segments could
be anywhere)
- Segments require GOTOs
6
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
7
Modern Multiple Selectors
case expression of
constant_list_1 : statement_1;
...
constant_list_n : statement_n
end
Design choices:
1. Expression is any ordinal type
(int, boolean, char, enum)
2. Segments can be single or compound
3. Construct is encapsulated
4. Only one segment can be executed per
execution of the construct
5. In Wirth's Pascal, result of an unrepresented
control expression value is undefined
(In 1984 ISO Standard, it is a runtime error)
7
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
2. The C and C++ switch
7
switch (expression) {
constant_expression_1 : statement_1;
...
constant_expression_n : statement_n;
[default: statement_n+1]
}
8
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
7
3. Ada's case is similar to Pascal's case, except:
9
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
7
Iterative Statements
- The repeated execution of a statement or
compound statement is accomplished either by
iteration or recursion; here we look at iteration,
because recursion is unit-level control
Counter-Controlled Loops
Design Issues:
10
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
1. FORTRAN 77 and 90 7
- Design choices:
1. Loop var can be INTEGER, REAL, or DOUBLE
2. Loop var always has its last value
3. The loop var 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
- Syntax:
[name:] DO variable = initial, terminal [, stepsize]
…
END DO [name]
11
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
2. ALGOL 60 7
- Syntax: for var := <list_of_stuff> do statement
where <list_of_stuff> can have:
- list of expressions
- expression step expression until expression
- expression while boolean_expression
12
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
3. Pascal 7
- Syntax:
for variable := initial (to | downto) final do
statement
- Design Choices:
1. Loop var must be an ordinal type of usual scope
2. After normal termination, loop var is undefined
3. The loop var cannot be changed in the loop; the
loop parameters can be changed, but they are
evaluated just once, so it does not affect loop
control
4. Just once
4. Ada
- Syntax:
13
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
Ada Design choices: 7
5. C
- Syntax:
for ([expr_1] ; [expr_2] ; [expr_3]) statement
14
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
- If the second expression7is absent, it is an infinite
loop
C Design Choices:
6. C++
- Differs from C in two ways:
1. The control expression can also be Boolean
2. The initial expression can include variable
definitions (scope is from the definition to
the end of the function in which it is defined)
7. Java
- Differs from C++ in two ways:
1. Control expression must be Boolean
2. Scope of variables defined in the initial
expression is only the loop body
15
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
Logically-Controlled 7
Loops
- Design Issues:
1. Pretest or postest?
2. Should this be a special case of the counting
loop statement (or a separate statement)?
- Language Examples:
16
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
7 Mechanisms
User-Located Loop Control
- Design issues:
Examples:
17
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
7
2. C , C++, and Java - break
Unconditional; for any loop or switch;
one level only (Java’s can have a label)
3. FORTRAN 90 - EXIT
Unconditional; for any loop, any number of
levels
18
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
7
- Perl has a built-in iterator for arrays and hashes
e.g.,
foreach $name (@names) { print $name }
Unconditional Branching
Problem: readability
Label forms:
19
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
Restrictions on Pascal's 7
gotos:
20
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
7 (Dijkstra, 1975)
Guarded Commands
Purpose: to support a new programming
methodology (verification during program
development)
21
Copyright © 1998 by Addison Wesley Longman, Inc.
Chapter
2. Loops 7 -> <statement>
do <boolean>
[] <boolean> -> <statement>
...
[] <boolean> -> <statement>
od
22
Copyright © 1998 by Addison Wesley Longman, Inc.