<<

Counter-Controlled Loops

CS 403 - Programming Design Issues: Languages 1. What is the type and of the loop var? 2. What is the value of the loop var at loop Class 20 termination? 3. Should it be legal for the loop var or loop October 30, 2001 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?

1

Control Flow Categories for Discussion Counter-Controlled Loops

Sequence (already done – trivial) 77 Syntax: DO label var = start, finish [, stepsize] Selection (did last Tuesday – pretty  Stepsize can be any value but zero easy)  Parameters can be expressions

Iteration Design choices: Skip procedures and recursion for now 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 the parameters can; because they are evaluated only once, it does not affect loop control 4. Loop parameters are evaluated only once

Iterative Statements Counter-Controlled Loops

Two general categories: FORTRAN 90’s Other DO  Counter-controlled Syntax:  Boolean-controlled [name:] DO variable = initial, terminal [, stepsize] … END DO [name]

Loop var must be an INTEGER Other design choices same as FORTRAN 77

1 Counter-Controlled Loops Counter-Controlled Loops Pascal Syntax: 5. (++): for variable := initial (to | downto) final do Syntax: statement for ([expr_1] ; [expr_2] ; [expr_3]) statement Design Choices: 1. Loop var must be an ordinal type of usual scope The expressions can be whole statements, or 2. After normal termination, loop var is undefined even statement sequences, with the statements 3. The loop var cannot be changed in the loop; the loop separated by commas parameters can be changed, but they are evaluated e.g., for (i = 0, j = 10; j == i; i++) ... just once, so it does not affect loop control If the second expression is absent, it is an infinite 4. Loop parameters evaluated just once (not once for every iteration) loop.

Counter-Controlled Loops Counter-Controlled Loops

4. Ada C Design Choices  There is no explicit loop var so no restriction on type. Syntax:  There is no explicit loop var so no restriction on scope  Everything can be changed in the loop for var in [reverse] discrete_range loop  The first expression is evaluated once, but the other two are evaluated with each iteration. ...  Expression 2 is evaluated at the beginning of each end loop iteration.  Expression 3 is evaluated at the end of each iteration.

Counter-Controlled Loops Counter-Controlled Loops

Ada Design choices 6. C++ 1. Type of the loop var is that of the discrete range; its scope is the loop body (it is Differs from C in two ways: implicitly declared) 1. The control expression can also be Boolean 2. The loop var does not exist outside the 2. The initial expression can include variable loop. declarations (scope is from the definition to the end of the loop body) 3. The loop var cannot be changed in the loop, but the discrete range can; changing the range does not affect loop control. 4. The discrete range is evaluated just once

2 User-Located Loop Control Counter-Controlled Loops Mechanisms

7. Java Design issues Differs from C++ in that the control expression 1. Should the conditional be part of the exit? must be Boolean. 2. Should the mechanism be allowed in an  Arbitrary integer expressions are not allowed as control expressions already controlled loop?  EX. In C++, you might do the following: 3. Should control be transferable out of for (p = head; p; p = p-> next) more than one loop?  Using p for the control expression would not be allowed in Java

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

3 User-Located Loop Control Exam Two (same format as Mechanisms before) 3. FORTRAN 90 - EXIT Unconditional; for any loop, any Separate compilation (3.6.2) number of levels Static vs. dynamic linking (9.7) Variable initialization issues (6.1) FORTRAN 90 also has CYCLE, which Expression evaluation: (6.1) has the same semantics as C's continue  Precedence, associativity, evaluation order  Short circuiting Selection statements: design issues (6.4) Iteration statements: design issues (6.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 colon) 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

Assignment #6

Position paper Due in two weeks CS 403 is a “W” course – so it’s either this or take another English class

4