Download presentation
Presentation is loading. Please wait.
Published byBethany Phyllis Barber Modified over 9 years ago
1
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands Sebesta Chapter 8
2
2 Levels 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
3
3 Evolution 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
4
4 Control Structures A control statement and the statements whose execution it controls 1.Sequence 2.Selection 3.Iteration/Repetition Overall Design Question –What control statements should a language have, beyond selection and pretest logical loops?
5
5 Flow Chart Elements decision block of statements control flow start/end
6
6 Sequence 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
7
7 Iteration/Repetition – Pre and Post Test Pre-test repetition statements true false condition ( while do ) true false statements condition Post-test repetition ( do while )
8
8 Minimum 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?
9
9 Selection Statements Alternatives between two or more execution paths 1. Two-way selectors 2. Multiple-way selectors
10
10 Two-Way Selection Statements General form: if then else 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?
11
11 Two-Way Selection: Fortran FORTRAN – IF ( ) Problem –only a single statement can be selected – GOTO must be used to select more, e.g. IF (.NOT. ) GOTO 20... 20 CONTINUE – GOTO s 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
12
12 Two Way-Selection in Common Lisp Syntax (if ) Single/nested functions for May contain multiple statements –if surrounded by a block structure prog, let, do, etc.
13
13 Lisp – 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")))
14
14 Nesting 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
15
15 Nesting 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
16
16 Multiple-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?
17
17 Multiple-Way Selection: Examples Early multiple selectors: –FORTRAN arithmetic IF (a three-way selector) IF (arithmetic expression) N1, N2, N3 –Segments require GOTO s (not structured programming) –Not encapsulated (selectable segments could be anywhere) Result: "spaghetti code"
18
18 Multiple-Way Selection: Examples Modern multiple selectors – switch statement in C, C++, Java switch ( ) { case : ; … case : ; [default: ] }
19
19 Multiple-Way Selection: Examples Design choices for C’s switch statement 1.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
20
20 Lisp 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 1 st true test and following expressions within the corresponding clause are evaluated –Safe - like ADA GOTOs not needed
21
21 Multiple-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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.