Statement-Level Control Structures - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Statement-Level Control Structures

Description:

Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 22
Provided by: Jan1194
Category:

less

Transcript and Presenter's Notes

Title: Statement-Level Control Structures


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
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
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
Control 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?

5
Flow Chart Elements
decision
block of statements
start/end
control flow
6
Sequence and Selection

Sequence
true
false

Selection (multi-way)
Selection (if/else)
7
Iteration/Repetition Pre and Post Test
Pre-test repetition
Post-test repetition
(while lttestgt do ltstuffgt)
(do ltstuffgt while lttestgt)
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
Selection Statements
  • Alternatives between two or more execution paths
  • 1. Two-way selectors
  • 2. Multiple-way selectors


10
Two-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?

11
Two-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

12
Two 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.

13
Lisp 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")))

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
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
Multiple-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?

17
Multiple-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"

18
Multiple-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

19
Multiple-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

20
Lisp 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

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
Write a Comment
User Comments (0)
About PowerShow.com