Chapter 8 Statement-Level Control Structures ISBN 0-321-33025-0 Chapter 8 Topics • Introduction • Selection Statements • Iterative Statements • Unconditional Branching Guarded Commands • Guarded Commands • Conclusions Copyright © 2006 Addison-Wesley. All rights reserved. 1-2 Introduction • Levels of Control Flow: 1. Within expressions 2. Among program statements 3. Among program units (highest level) • Evolution: – FORTRAN I control statements were based directly on IBM 704 hardware – It was proven that all flowcharts can be coded with only two-way selection and pretest logical Copyright © 2006 Addison-Wesley. All rights reserved. 1-3 loops Introduction (cont.) •A control structure is a control statement and the collection of statements whose execution it controls • Overall Design Question: What control statements should a language have – What control statements should a language have , beyond selection and pretest logical loops? Copyright © 2006 Addison-Wesley. All rights reserved. 1-4 Design issues • Whether the control structure can have multiple Whether the control structure can have multiple entries Whether the control structure can have multiple • Whether the control structure can have multiple exits Copyright © 2006 Addison-Wesley. All rights reserved. 1-5 Compound statements • Compound statements allow a collection of statements to be abstracted to a single statement statement • Data declarations can be added to the bii f dtt ti l b eg i nn i ng o f a compoun d s t a t emen t i n severa l languages, making it a block Copyright © 2006 Addison-Wesley. All rights reserved. 1-6 Selection Statements • A selection statement provides the means of A selection statement provides the means of choosing between two or more paths of execution execution • Two general categories: T lt – T wo-way se l ec t ors – Multiple-way selectors Copyright © 2006 Addison-Wesley. All rights reserved. 1-7 Two-Way Selection Statements • Design issues: – The type of expression that controls the selector – Whether single statements, compound Whether single statements, compound statements, or statement sequences can be selected selected – How should the meaning of nested selectors be specified specified Copyright © 2006 Addison-Wesley. All rights reserved. 1-8 Example – FORTRAN IV • FORTRAN IF IF (boolean_expr) statement • Problem: can select only a single statement; to Problem: can select only a single statement; to select more, a GOTO must be used, as in the following example following example IF (FLAG .NE. 1) GO TO 20 I1 I = 1 ; J = 2; Copyright © 2006 Addison-Wesley. All rights reserved. 1-9 20 CONTINUE Example – Algol 60 • Syntax: if (Boolean expression) then statement statement else statement • The statements could be single or compound Copyright © 2006 Addison-Wesley. All rights reserved. 1-10 Nested Selectors - if … if … else • Java's static semantics rule: else goes with the nearest if • ALGOL 60 chose to use syntax, rather than a ALGOL 60 chose to use syntax, rather than a rule, to connect else clauses to then clauses if sum = 0 then begin if count = 0 then result : = 0 if sum = 0 then begin if count = 0 then result : = 0 result : = 0 else result := 1 d result : = 0 end else lt 1 Copyright © 2006 Addison-Wesley. All rights reserved. 1-11 en d resu lt := 1 end Nested Selectors (cont.) • FORTRAN 90 and Ada solution – closing special words – Advantage: readability – Example: in Ada if then if then if then if then l if then if then dif e l se end if dif en d if else dif Copyright © 2006 Addison-Wesley. All rights reserved. 1-12 en d if en d if Multiple-Way Selection Statements •The multiple selection construct allows the selection of one of any number of statements selection of one of any number of statements • Design issues: Wh t i th f d t f th t l – Wh a t i s th e f orm an d t ype o f th e con t ro l expression? How are the selectable segments specified (single – How are the selectable segments specified (single , compound or statement sequences)? – Is execution flow through the structure restricted Is execution flow through the structure restricted to include just a single selectable segment? – What is done about unrepresented expression Copyright © 2006 Addison-Wesley. All rights reserved. 1-13 values? Early Multiple Selectors – Fortran I • Syntax: (i)1 2 3 IF ( express i on ) <LABEL 1 > , <LABEL 2 >, <LABEL 3> <LABEL1> … GO TO <LABEL > <LABEL2> … GO TO <LABEL> <LABEL3 > … … <LABEL> <LABEL> • Bad aspects: – Harmful to readabilit y Copyright © 2006 Addison-Wesley. All rights reserved. 1-14 y – A trade-off of reliability for some added flexibility Modern Multiple Selectors • The general form of Hoare's multiple selector is iif case i nteger _ express i on o f begin statement 1 ; _; statement_2; … statement _ n; end where: where: –The statement_i could be either single statements or compound statements Th d i h h b h Copyright © 2006 Addison-Wesley. All rights reserved. 1-15 – Th e execute d statement i s t h e one c h osen b y t h e value of the integer_expression Design choices • Expression is any ordinal type (int, boolean, char, enum) • Segments can be single, compound statement or Segments can be single, compound statement or block O/ b f t() b td • O ne / any num b er o f segmen t( s ) can b e execu t e d per execution of the construct • Many dialects now have otherwise or else clause for unrepresented values Copyright © 2006 Addison-Wesley. All rights reserved. 1-16 Example - Pascal case idx of if index = 1 goto one _ three 1, 3: begin odd := odd + 1; s odd : = s odd + idx; _ if index = 3 goto one_three if index = 2 goto two_four if index = 4 g oto two four s _ odd : s _ odd + idx; end; 2, 4: begin 1 g _ writeln(‘Error’) goto out one three: even := even + 1 ; s_even := s_even + idx; end; _ odd := odd + 1; s_odd := s_odd + index; goto out else Writeln(‘Error’); end; goto out two_four: even := even + 1; s even := s even + index; Copyright © 2006 Addison-Wesley. All rights reserved. 1-17 s _ even := s _ even + index; out: … Multiple-Way Selection Statements • Multiple selectors can appear as direct i li extens i ons to two-way se l ectors, us i ng else-if clauses (ALGOL 68, FORTRAN 90, Ada, C) • Example: In Ada if then elsif then elsif then else end if Copyright © 2006 Addison-Wesley. All rights reserved. 1-18 end if Iterative Statements • The repeated execution of a statement or dtt ti lihdith b compoun d s t a t emen t i s accomp li s h e d e ith er b y iteration or recursion lk b • Here we l oo k at iteration, b ecause recursion is unit-level control • General design issues for iteration control statements: 1. How is iteration controlled? 2. Where should the control mechanism appear in Copyright © 2006 Addison-Wesley. All rights reserved. 1-19 the loop? Counter-Controlled Loops •A counting iterative control statement has a variable, called the loop variable, in which the count value is maintained • Loop parameter includes the initial, terminal values of the loop variable and stepsize -the difference between sequential loop variable values • Counter-controlled loops are often supported by machine instructions so their execution is Copyright © 2006 Addison-Wesley. All rights reserved. 1-20 faster than logically-controlled loops [...]... many cases, collections of statements must be repeatedly executed, but the repetition control is based on a Boolean expression rather than a counter h • Logically-controlled loops are more general than counter-controlled loops Every counting loop can be built with a logical loop, but the reverse is not true • Only selection and logical loops are essential to express the control structure of any flowchart... reserved 1-32 Iterative Statements – C++, Java • C++: differs from C in two ways – The control expression can also be Boolean – The initial expression can include variable definitions (scope is from the definition to the end of the loop body) • Java: differs from C++ in that the control expression must b B l i t be Boolean Copyright © 2006 Addison-Wesley All rights reserved 1-33 Logically-Controlled Loops... for any loop or switch; one level only – continue: Unconditional, for any loops; it skips the remainder of this iteration, but does not exit the loop h l • FORTRAN 90 – EXIT: Unconditional; for any loop, any number of levels – CYCLE: the same semantics as C's continue Copyright © 2006 Addison-Wesley All rights reserved 1-39 Iteration Based on Data Structures • These loops are controlled by the number... loop2 out: … 1-27 Iterative Statements - Pascal • Syntax: for var := initial (to | downto) final do statement • Design Choices: – Loop variable must be an ordinal type – After normal termination, loop variable is undefined p g p; – The loop variable 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 – Stepsize is 1 Copyright... Addison-Wesley All rights reserved 1-34 Design Issues • Pretest or posttest • Should this be a special case of the counting loop statement (or a separate statement) Copyright © 2006 Addison-Wesley All rights reserved 1-35 Language Examples • C/C++ also have both, but the control expression for the posttest version is treated just like in the pretest case (while-do and do-while) Pretest l i l l P t... out: … Copyright © 2006 Addison-Wesley All rights reserved 1-23 Iterative Statements – Algol 60 • EBNF notation: for var := {, } do | step until | while • Design choices: – Control expression can be i t or real int l – Control variable has its last assigned value after loop termination – The... • This methodology is described in Dijkstra (1976) Copyright © 2006 Addison-Wesley All rights reserved 1-43 Selection • Syntax: if B l i t t t [] … [] fi • Semantics: when this construct is reached – Evaluate all boolean expressions p – If more than one are true, choose one nondeterministically... + i if [] fi x >= y –> max := x y >= x –> max := y Copyright © 2006 Addison-Wesley All rights reserved 1-46 Loops • Syntax: do B l d i t t t [] … [] od • Semantics: For each iteration – Evaluate all boolean expressions – If more than one are true, choose one nondeterministically; then start loop... 1-37 User-Located Loop Control Mechanisms • Ada loop loop Name: if cond then exit when cond; loop exit; end loop; exit Name when end if; found; end loop; end loop Name; loop loop Name: … … loop if s > 5 then exit when S > 5; … exit; exit Name when … S > 5; end if; end loop; … … end loop Name; end loop; p p Copyright © 2006 Addison-Wesley All rights reserved 1-38 User-Located Loop Control Mechanisms •... out for_var = [next element of discrete range] [loop b d ] [l body] goto loop out: [undefined for_var] Copyright © 2006 Addison-Wesley All rights reserved 1-30 Iterative Statements – C • Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) statement • Design choices: – There is no explicit loop variable – Everything can be changed in the loop – The first expression is evaluated once, but the other two are evaluated . (cont.) •A control structure is a control statement and the collection of statements whose execution it controls • Overall Design Question: What control statements should a language have – What control . issues • Whether the control structure can have multiple Whether the control structure can have multiple entries Whether the control structure can have multiple • Whether the control . iteration, b ecause recursion is unit-level control • General design issues for iteration control statements: 1. How is iteration controlled? 2. Where should the control mechanism appear in Copyright