Control Structures sequence of execution of high-level statements
logic/ functional addressing encoding datainstructions data manipulation control location calculation identifiers/ names data types ADTs expressions assignment objects subprograms concurrency exception handling control structures COSC 3217 Programming Languages Dave Goforth
Control is control of sequence of execution machine level – manipulate address register: (un)conditional branch high level branch to label goto GOTO GO TO constrained branch: break high level: selection, iteration
Control by branching - FORTRAN IF(X.EQ. Y) X=X+1 IF(X.EQ. Y) GO TO 100 > GO TO CONTINUE (dummy statement) > 200 CONTINUE >
Control by branching - FORTRAN IF(X-Y) 10,10,20 10 CONTINUE << statements for X<=Y GO TO CONTINUE Y >> 30 CONTINUE >
Statement labels integers: FORTRAN, Pascal identifiers as constants: ALGOL, C variables! PL/1
The ‘goto’ debate: is branching needed? powerful: only control needed easy to abuse: readability, reliability suffer ‘goto’ unnecessary (Böhm and Jacopini, 1966) but most languages retain limited (unlabelled) branching – e.g., break
Top-down structured programming 1975 flow chart model: no goto 3 flow structures -sequence -if-then-else (selection) -do-while (iteration) boolean expr statement
Selection: either / or how many alternatives: 1do something or nothing 2if then else 3IF (X-Y) 10,20,30 manyswitch, nested IF, COND (LISP) variations on the selection structure
Selection design decisions control of multiple statements in the control statement compound statement / block
Selection design decisions - if nesting selection – syntax and logic convention/rule java, etc syntax – use block if (sum==0) //p.314 ? if (count==0) result = 0; else result = 1;
Multiple selection (1) FORTRAN computed goto is multiple branch GO TO (10,20,30,40,50,60), X^^2+Y case / switch model design decisions default case allowed/ignored? branch after section?
Multiple selection (2) guarded statements – Dijkstra evaluate all conditions: none true – error one true – do statement many true – select statement at random if -> [] -> fi
Multiple selection (2) Ada: if (condition) elsif (condition) elsif (condition) else LISP (COND (( condition )(value)) (( condition )(value)) (( condition )(value)) ( T(value)))
Iteration: repetition of statements control by counter - for loop control by data (by logic) - while loop number of executions: 0 or more - pretest at beginning of loop body 1 or more - posttest at end of loop body branching models of repetition
Counter controlled loops developed with array indexing counter (loop variable) parameters start, stop, stepsize
Counter controlled loops design decision parameters computed when once or every execution ? x = 10; do I = 0 to x x = x executions or 6 executions
Counter controlled loops design decisions: loop variable type, scope and value x = 0; I = 100; do I = 1 to 10 by 3 begin x = x + I; I = I - 1; print I end; print I, x;
Counter controlled loops FORTRAN DO loop posttest, parameters evaluated once, number of executions predetermined, loop variable retains last value DO 100 K = 1, 10, 2 > 100 CONTINUE
Generalizing the counter start, stop, step - FORTRAN sequence, index expression & condition, “all of the above” - ALGOL for i = 4,5,6 i*10 while i,10000, 12, 14 step 10 until 25 do simple counting – Ada, Pascal expressions – C,C++,java for (,, )
Logical control design issue: pretest or posttest? most languages have ‘while’ pretest loop posttest: PascalC sum := 0;sum = 0; repeatdo sum := sum + 15 sum += 15; until (sum > 100); while (sum <= 100);
Constrained branching switch statement break programmer control of loop when to exit (between start and end) branch to end or branch out orthogonality – separate repetition (infinite loop) from branching out