1 statement-level control structures levels of flow control control statements 1. sequence 2....
Post on 26-Dec-2015
217 Views
Preview:
TRANSCRIPT
1Statement-Level Control Structures
• Levels of flow control• Control Statements
1. Sequence
2. Selection
3. Iteration
• Unconditional branching• Guarded commands• Sebesta Chapter 8
2Levels of Program Flow Control
1. Within expressions – Ch. 7
2. Among program statements – Ch. 8
3. Among program units – Ch. 9-10
4. Exceptions – Ch. 14
3Evolution of Flow Control
• FORTRAN I – control statements based directly on IBM 704
hardware
• 1960s – A lot of research and argument about different types
of flow control
4Control Structures
• A control statement and the statements whose execution it controls1. Sequence
2. Selection
3. Iteration/Repetition
• Overall Design Question– What control statements should a language have,
beyond selection and pretest logical loops?
5Flow Chart Elements
decisionblock of
statements
control flow
start/end
6Sequence and Selection
Sequence
Selection (if/else)
Selection (multi-way)
true statements x break case x
false
default statements
true statements a break case a
false
true statements b break case b
false
true false…
condition
statement a statement b
7Iteration/Repetition – Pre and Post Test
Pre-test repetition
statements
true
false condition
(while <test> do <stuff>)
true
false
statements
condition
Post-test repetition
(do <stuff> while <test>)
8Minimum Control Structures
• Proven early:– all control structures can be coded with only
1. two-way selection, and
2. pretest logical loops!
• Lecture-specific question:– What is the relation to assembly languages?
9Selection Statements
• Alternatives between two or more execution paths 1. Two-way selectors
2. Multiple-way selectors
10Two-Way Selection Statements
• General form:if <control_expression>
then <then_clause>
else <else_clause>
• Design Issues:– What is the form and type of the control expression?– How are then_clause and else_clause specified?– How should the meaning of nested selectors be specified?
11Two-Way Selection: Fortran
• FORTRAN– IF (<boolean_expr>) <statement>
• Problem– only a single statement can be selected– GOTO must be used to select more, e.g.
IF (.NOT. <condition>) GOTO 20
...
20 CONTINUE– GOTOs must also be used for the else clause– Negative logic is bad for readability
• These problems were solved in FORTRAN 77• Most later languages
– allow compound statements to be selected– support else clause
12Two Way-Selection in Common Lisp
• Syntax(if <test> <do_if_true>
<do_if_false>)
• Single/nested functions for <do_if_...>
• May contain multiple statements – if surrounded by a block structure
• prog, let, do, etc.
13Lisp – Two Way Selection
(when (= x 0)
(terpri)
(princ "It's now 0")
)
(if (> (aref a x) (aref b x))
(progn ; block, returns last value
(terpri) (princ "Fixing")
(setf (aref a x) (aref b x)))
(if (< (aref a x) (aref b x))
(format t "~%It's Less")
(format t "~%It's Equal")))
14Nesting Selectors
• Java exampleif (sum == 0)
if (count == 0)
result = 0;
else result = 1;
• To which if does the else belong to?
• Java's static semantics rule– else matches with the nearest if
15Nesting Selectors (cont.)
• Compound statements must be used for alternative semantics
if (sum == 0) {
if (count == 0)
result = 0;
} else result = 1;
• {} are used in C, C++, and C#
• Perl: all then and else clauses must be compound
16Multiple-Way Selection Statements
• Select among one of many statements
• Design issues:1. Form and type of the control expressions
2. How are the selectable segments specified?
3. Will only a single segment be executed or does control continue checking other control expressions?
4. What happens if no control expressions occurs?
17Multiple-Way Selection: Examples
• Early multiple selectors:
– FORTRAN arithmetic IF (a three-way selector)
IF (arithmetic expression) N1, N2, N3
– Segments require GOTOs (not structured programming)
– Not encapsulated (selectable segments could be anywhere)
• Result: "spaghetti code"
18Multiple-Way Selection: Examples
• Modern multiple selectors
– switch statement in C, C++, Javaswitch (<expression>) {
case <const_expr_1>: <stmt_1>;
…
case <const_expr_n>: <stmt_n>;
[default: <stmt_n+1>]
}
19Multiple-Way Selection: Examples
• Design choices for C’s switch statement1. Control expression can be only an integer type
2. Selectable segments can be statement sequences
3. Any number of segments can be executed in one execution of the construct• no implicit branch at the end of selectable segments
4. default clause is for unrepresented values • if there is no default, the whole statement does nothing
– This leads to many bugs
20Lisp Multiple Selection – cond
(defun test (x)
(cond
((< x 0) ;test
(terpri) (princ “It’s negative"))
; actions to perform if test is true
((> x 100)
(terpri) (princ "It's huge"))
(t
(terpri) (princ “It’s reasonable"))))
• Only the 1st true test and following expressions within the corresponding clause are evaluated
– Safe - like ADA
• GOTOs not needed
21Multiple-Way Selection With if
• Multiple Selectors can appear as direct extensions of two-way selectors, using else-if clauses, e.g., in Ada:if ...
then ...
elsif ...
then ...
elsif ...
then ...
else ...
endif
top related