Title: Statement-Level Control Structures
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 controls - Sequence
- Selection
- Iteration/Repetition
- Overall Design Question
- What control statements should a language have,
beyond selection and pretest logical loops?
5Flow Chart Elements
decision
block of statements
start/end
control flow
6Sequence and Selection
Sequence
true
false
Selection (multi-way)
Selection (if/else)
7Iteration/Repetition Pre and Post Test
Pre-test repetition
Post-test repetition
(while lttestgt do ltstuffgt)
(do ltstuffgt while lttestgt)
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 ltcontrol_expressiongt
- then ltthen_clausegt
- else ltelse_clausegt
- 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 (ltboolean_exprgt) ltstatementgt
- Problem
- only a single statement can be selected
- GOTO must be used to select more, e.g.
- IF (.NOT. ltconditiongt) 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 lttestgt ltdo_if_truegt
- ltdo_if_falsegt)
- Single/nested functions for ltdo_if_...gt
- 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 (gt (aref a x) (aref b x))
- (progn block, returns last value
- (terpri) (princ "Fixing")
- (setf (aref a x) (aref b x)))
- (if (lt (aref a x) (aref b x))
- (format t "It's Less")
- (format t "It's Equal")))
14Nesting Selectors
- Java example
- if (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
- Form and type of the control expressions
- How are the selectable segments specified?
- Will only a single segment be executed or does
control continue checking other control
expressions? - 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, Java
- switch (ltexpressiongt) case ltconst_expr_1gt
ltstmt_1gt -
- case ltconst_expr_ngt ltstmt_ngt
- default ltstmt_n1gt
19Multiple-Way Selection Examples
- Design choices for Cs switch statement
- Control expression can be only an integer type
- Selectable segments can be statement sequences
- Any number of segments can be executed in one
execution of the construct - no implicit branch at the end of selectable
segments - 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
- ((lt x 0) test
- (terpri) (princ Its negative"))
- actions to perform if test is true
- ((gt x 100)
- (terpri) (princ "It's huge"))
- (t
- (terpri) (princ Its 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