DePaul CSC 447 - Statement-Level Control Structures

Unformatted text preview:

ISBN 0-321-33025-0Chapter 8Chapter 8Chapter 8Chapter 8Statement-Level Control StructuresCopyright © 2006 Addison-Wesley. All rights reserved. 1-2Chapter 8 Topics• Introduction• Selection Statements• Iterative Statements• Unconditional Branching• Guarded Commands• ConclusionsCopyright © 2006 Addison-Wesley. All rights reserved. 1-3Levels of Control Flow– Within expressions– Among program units– Among program statementsCopyright © 2006 Addison-Wesley. All rights reserved. 1-4Control Statements: Evolution• FORTRAN I control statements were based directly on IBM 704 hardware• Much research and argument in the 1960s about the issue– One important result: It was proven that all algorithms represented by flowcharts can be coded with only two-way selection and pretest logical loopsCopyright © 2006 Addison-Wesley. All rights reserved. 1-5Control Structure• A control structureis a control statement and the statements whose execution it controls • Design question– Should a control structure have multiple entries?Copyright © 2006 Addison-Wesley. All rights reserved. 1-6Selection Statements• A selection statementprovides the means of choosing between two or more paths of execution• Two general categories:– Two-way selectors– Multiple-way selectorsCopyright © 2006 Addison-Wesley. All rights reserved. 1-7Two-Way Selection Statements• General form:if control_expressionthen clauseelse clause• Design Issues:– What is the form and type of the control expression?– How are the thenand elseclauses specified?– How should the meaning of nested selectors be specified?Copyright © 2006 Addison-Wesley. All rights reserved. 1-8Two-Way Selection: Examples• FORTRAN: IF(boolean_expr) statement• Problem: can select only a single statement; to select more, a GOTOmust be used, as in the following exampleIF (.NOT. condition) GOTO 20...20 CONTINUE• Negative logic is bad for readability• This problem was solved in FORTRAN 77• Most later languages allow compounds for the selectable segment of their single-way selectorsCopyright © 2006 Addison-Wesley. All rights reserved. 1-9Two-Way Selection: Examples• ALGOL 60: if (boolean_expr)then statement (then clause)else statement (else clause)• The statements could be single or compoundCopyright © 2006 Addison-Wesley. All rights reserved. 1-10Nesting Selectors• Java exampleif (sum == 0)if (count == 0)result = 0;else result = 1;• Which if gets the else? • Java's static semantics rule: else matches with the nearest ifCopyright © 2006 Addison-Wesley. All rights reserved. 1-11Nesting Selectors (continued)• To force an alternative semantics, compound statements may be used:if (sum == 0) {if (count == 0)result = 0;}else result = 1;• The above solution is used in C, C++, and C#• Perl requires that all then and else clauses to be compoundCopyright © 2006 Addison-Wesley. All rights reserved. 1-12Multiple-Way Selection Statements• Allow the selection of one of any number of statements or statement groups• Design Issues:1. What is the form and type of the control expression?2. How are the selectable segments specified?3. Is execution flow through the structure restricted to include just a single selectable segment?4. What is done about unrepresented expression values?Copyright © 2006 Addison-Wesley. All rights reserved. 1-13Multiple-Way Selection: Examples• Early multiple selectors:– FORTRAN arithmetic IF (a three-way selector)IF (arithmetic expression) N1, N2, N3Where the N values are labels that direct control when the value of the expression is greater than, equal to, and less than 0 respectively.– Segments require GOTOs– Not encapsulated (selectable segments could be anywhere)Copyright © 2006 Addison-Wesley. All rights reserved. 1-14Multiple-Way Selection: Examples• Modern multiple selectors– C’s switch statementswitch (expression) {case const_expr_1: stmt_1;…case const_expr_n: stmt_n;[default: stmt_n+1]}Copyright © 2006 Addison-Wesley. All rights reserved. 1-15Multiple-Way Selection: Examples• Design choices for C’s switch statement1. Control expression can be only an integer type2. Selectable segments can be statement sequences, blocks, or compound statements3. Any number of segments can be executed in one execution of the construct (there is 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)Copyright © 2006 Addison-Wesley. All rights reserved. 1-16Multiple-Way Selection: Examples• The Ada case statementcase expression iswhen choice list => stmt_sequence;…when choice list => stmt_sequence;when others => stmt_sequence;]end case;• More reliable than C’s switch(once a stmt_sequence execution is completed, control is passed to the first statement after the case statementCopyright © 2006 Addison-Wesley. All rights reserved. 1-17Multiple-Way Selection Using if• Multiple Selectors can appear as direct extensions to two-way selectors, using else-if clauses, for example in Ada:if ...then ...elsif ...then ...elsif ...then ...else ...end ifCopyright © 2006 Addison-Wesley. All rights reserved. 1-18Iterative Statements• The repeated execution of a statement or compound statement is accomplished either by iteration or recursion • General design issues for iteration control statements:1. How is iteration controlled?2. Where is the control mechanism in the loop?Copyright © 2006 Addison-Wesley. All rights reserved. 1-19Counter-Controlled Loops• A counting iterative statement has a loop variable, and a means of specifying the initialand terminal, and stepsizevalues• Design Issues:1. What are the type and scope of the loop variable?2. What is the value of the loop variable at loop termination?3. Should it be legal for the loop variable or loop parameters to be changed in the loop body, and if so, does the change affect loop control?4. Should the loop parameters be evaluated only once, or once for every iteration?Copyright © 2006 Addison-Wesley. All rights reserved. 1-20Iterative Statements: Examples• FORTRAN 90 syntaxDO label var = start, finish [, stepsize]• Stepsize can be any value but zero• Parameters can be expressions• Design choices:1. Loop variable must be INTEGER2. Loop variable always has its last value3. The loop variable cannot be changed in the loop, but the parameters can; because they are evaluated only once, it does not affect loop


View Full Document

DePaul CSC 447 - Statement-Level Control Structures

Download Statement-Level Control Structures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Statement-Level Control Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Statement-Level Control Structures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?