<<

Types of Selection Statements CS 403 - Programming Single-way Languages Two way Multiple selection (number of branches Class 18 typically greater than 2) October 23, 2001

1

Control Flow Categories for Single Way Selection Discussion Statements: As in ++

Sequence (already done – trivial) Controlling a single statement: if (boolean_expr) Selection Single statement; Iteration Controlling a block of statements: Procedures if (boolean_expr){ statement1; Recursion statement2; statement3; }

Blocks and Compound Statements Two-way Selector Examples

• Compound statements - introduced C++ if: by ALGOL 60 in the form of begin...end if (boolean_expr) then statement (the then clause) else statement (the else clause) • A block is a compound statement that can define a new (with • The statements could be single or local variables) compound.

1 Nested Selectors Design Issues for Switch/Case e.g. (Pascal) if ... then if ... then ... Do you allow multiple values at a branch? else ... Alternatives:  case 1: case2: case 3: Which then gets the else?  Subranges: case 10..15 Pascal's rule: else goes with the nearest then  Use of Boolean OR operator case 1..5 | 7 | 15..20 Must the constants be exhaustive? • ALGOL 60's solution - disallow direct nesting  Often accomplished with default clause  In C++ or Java, you don’t have to cover every if ... then if ... then begin begin case (don’t have to have a default clause) if ... if ... then ...  Default clause increases reliability; otherwise then ... end the entire statement might be skipped. else ... else ... End

Multiple Selection Constructs If-Elseif Ada: Switch/case if ... then ... Multi-way if/elseif elsif ... then ... elsif ... then ... else ... end if Far more readable than deeply nested if's Allows a boolean condition on every selectable group

C++ switch statement Iterative Statements

switch (b){ Two general categories: case 0: x++; y++; break;  Counter-controlled case 1:  Boolean-controlled z++, q++; break; case 2: ….. default: ++other_cnt; } Java “switch” statement is similar Other languages have “case” statements that work similarly

2 Counter-Controlled Loops Counter-Controlled Loops Pascal Design Issues: Syntax: 1. What is the type and scope of the loop var? for variable := initial (to | downto) final do statement What is the value of the loop var at loop 2. Design Choices: termination? 1. Loop var must be an ordinal type of usual scope 3. Should it be legal for the loop var or loop 2. After normal termination, loop var is undefined parameters to be changed in the loop body, and 3. The loop var cannot be changed in the loop; the loop if so, does the change affect loop control? parameters can be changed, but they are evaluated 4. Should the loop parameters be evaluated only just once, so it does not affect loop control once, or once for every iteration? 4. Loop parameters evaluated just once (not once for every iteration)

Counter-Controlled Loops Counter-Controlled Loops

FORTRAN 77 4. Ada Syntax: DO var = start, finish [, stepsize]  Stepsize can be any value but zero  Parameters can be expressions Syntax:

Design choices: for var in [reverse] discrete_range loop 1. Loop var can be INTEGER, REAL, or DOUBLE 2. Loop var always has its last value ... 3. The loop var cannot be changed in the loop, but end loop the parameters can; because they are evaluated only once, it does not affect loop control 4. Loop parameters are evaluated only once

Counter-Controlled Loops Counter-Controlled Loops

Ada Design choices 90’s Other DO 1. Type of the loop var is that of the discrete range; its scope is the loop body (it is Syntax: implicitly declared) [name:] DO variable = initial, terminal [, stepsize] 2. The loop var does not exist outside the … loop. END DO [name] 3. The loop var cannot be changed in the loop, but the discrete range can; changing the Loop var must be an INTEGER range does not affect loop control. Other design choices same as FORTRAN 77 4. The discrete range is evaluated just once

3 Counter-Controlled Loops Counter-Controlled Loops

5. C(++): 7. : Differs from C++ in that the control expression for ([expr_1] ; [expr_2] ; [expr_3]) statement must be Boolean. The expressions can be whole statements, or  Arbitrary integer expressions are not allowed as even statement sequences, with the statements control expressions separated by commas  EX. In C++, you might do the following: for (p = head; p; p = p-> next) e.g., for (i = 0, j = 10; j == i; i++) ...  Using p for the control expression would not be If the second expression is absent, it is an infinite allowed in Java loop.

Counter-Controlled Loops A Skill You Should Develop

C Design Choices Given a BNF description and a  There is no explicit loop var so no restriction on type. description of the semantics of  There is no explicit loop var so no restriction on something (e.g., a loop), you should be scope  Everything can be changed in the loop able to write a correctly functioning  The first expression is evaluated once, but the program in a particular programming other two are evaluated with each iteration.  Expression 2 is evaluated at the beginning of each language. iteration.  Expression 3 is evaluated at the end of each iteration.

Counter-Controlled Loops Exercise

6. C++ Rewrite in Ada & C/C++ & Java Differs from C in two ways: k = (j + 13) / 27 1. The control expression can also be Boolean loop: 2. The initial expression can include variable if k > 10 then out declarations (scope is from the definition to the k := k + 1 end of the loop body) i := 3 * k – 1 goto loop out:

4 User-Located Loop Control Logically-Controlled Loops Mechanisms 1. Ada - conditional or unconditional; for any loop; While loops and their variants any number of levels Design issue: Pre-test (condition for ... loop LOOP1: ... while ... loop tested at beginning) or post-test exit when ...... (condition tested at end) ... LOOP2: end loop for ... loop ... exit LOOP1 when .. ... end loop LOOP2; ... end loop LOOP1;

User-Located Loop Control Logically-Controlled Loops Mechanisms 2. C , C++, and Java - break Language Examples: Unconditional; for any loop or switch; 1. Pascal has separate pretest and posttest logical loop statements (while-do and repeat-until) one level only (Java’s can have a label) 2. C and C++ also have both, but the control expression for the posttest version is treated just like in the pretest case (while - do and do - There is also has a continue statement for while) loops; it skips the remainder of this 3. Java is like C, except the control expression iteration, but does not exit the loop must be Boolean (and the body can only be entered at the beginning--Java has no goto) 4. Ada has a pretest version, but no posttest 5. FORTRAN 77 and 90 have neither

User-Located Loop Control User-Located Loop Control Mechanisms Mechanisms 3. FORTRAN 90 - EXIT Design issues Unconditional; for any loop, any 1. Should the conditional be part of the exit? number of levels 2. Should the mechanism be allowed in an already controlled loop? FORTRAN 90 also has CYCLE, which has the same semantics as C's continue 3. Should control be transferable out of more than one loop?

5 Unconditional Branching The “Goto” statement Problem: readability Some languages do not have them:e.g., Modula-2 and Java Label forms: 1. Unsigned int constants: Pascal (with ) FORTRAN (no colon) 2. Identifiers with colons: ALGOL 60, C 3. Identifiers in << ... >>: Ada 4. Variables as labels: PL/I  Can be assigned values and passed as parameters  Highly flexible, but make programs impossible to read and difficult to implement

6