Flow of Control Execution Sequence Sequence sequence Decision tree Repetition graph Language Constructs sequence statements (incl Foc) simple / compound decision (1 /2 / n way) if then [else] case / switch repetition for loop (counter) while (condition) * repeat (do) / until (cond) + Recursion 3AC: if X goto L / goto L 1DFR - PL - Flow of Control
Control Structures Control structure control statement + collection of statements single entry & exit Compound statement e.g. begin statement_1 statement_2 statement_n end Block compound statement + data declarations (e.g. C) Decision selection statements 1 way Fortran IV IF (Boolean expr) statement 2 way if then else n way case / switch 2DFR - PL - Flow of Control
2 way Decision: if then else dangling else problem if A then if B then C else D syntactic/semantic solution? if A then { if B then C else D } if A then { if B then C } else D or if A then if B then C else D use a special word end if Algol 68, Fortran 77/90, Ada Modula 2 uses end only less readable Operational Semantics if A then B else C if not A goto Lelse B goto Lout Lelse: C Lout: NB Three address code (3ac) conditional goto if A then goto L unconditional goto goto L 3DFR - PL - Flow of Control
Multiple Selection Examples Fortran IF (arith expr) N1 N2 N3 others case <expr> of <0 0 >0 const_list_1: statement_1; const_list_2: statement_2; const_list_n: statement_n; else default_statement end {case} Operational Semantics if I = 1 goto L1 if I = 3 goto L3 if I = n goto Ln default statement; goto Lout L1: statement_1; goto Lout... Ln: statement_n; goto Lout default: statement Lout: NB C switch & break 4DFR - PL - Flow of Control
Multiple Selection : Design Issues Design Issues (multiple selection) type of case expression ordinal case selectors disjoint sets? multiple entries e.g. 1, 3, 6: statement_x are ranges allowed? e.g.1..10: statement_x default case mandatory? ex/implicit branch (C/Pascal) Cascaded if then else Boolean rather than ordinal if A then S1 else if B then S2 else if X then Sn if A then S1 (Ada) elsif B then S2 elsif X then Sn 5DFR - PL - Flow of Control
Repetition (Iteration, Loop) Loop = test + body pre test (before body executes)* post test (after body executes)+ Counter controlled loops n iterations loop parameters loop variable initial value terminal value step size Design Issues loop variable allowable types ordinal (int, char), real? scope? value on termination? can loop parameters be changed in the body? are loop parameters evaluated once or at every iteration? 6DFR - PL - Flow of Control
Loop Examples (historic) Fortran 77 DO label var = init, term, stepsize operational semantics iv := init initial value tv := term terminal value sv := stepsize step size dov := initval do variable ic := max( (tv iv + sv) / sv, 0) loop: if ic <= 0 goto lout [loop body] dov = dov + sv ic = ic 1 goto loop lout: Algol 60 <for_stmt> ::= for var := <list_el> {, <list_el> } do <stmt> <list_el> ::= <expr> <expr> step <expr> until<expr> <expr> while <Boolean_expr> for c := 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 do for c := 1 step 1 until 10 do for c := 1, c+1, while (c <= 10) do NB all <expr>s in for are evaluated for every iteration; loop variable may be changed in body 7DFR - PL - Flow of Control
Loop Examples Pascal for var := init_value (to downto) final_value do statement semantics loop variable ordinal type value undefined on normal termination last value on abnormal termination may not be changed in the body init_/ final_value evaluated once C, C++, Java for (expr1; expr2; expr3) loop body operational semantics expr1 loop: if expr2 = 0 goto lout [loop body] expr3 goto loop lout: e.g. for ( i=0; i<limit; i++)... 8DFR - PL - Flow of Control
Logically Controlled Loops Design issues pre test or post test? is this a special form of a counting loop or separate? Examples C++ while (<expr>) [loop body] do [loop body] while (<expr>) Operational semantics while loop: if!<expr> goto lout [loop body] goto loop lout: do while loop: [loop body] if <expr> goto loop 9DFR - PL - Flow of Control
Other Control Constructs Ada loop exit (also other PLs) loop end loop exit [loop label] [when <cond>] (un)conditional loop exit design issues is <cond> part of exit? where is exit allowed? in controlled loops? anywhere? is exit from one loop or several nested loops? C, C++ continue statement skips part of loop body exit, break allow multiple exits Data structure based iteration $ns = ( Bob, Ted, Joy ) foreach $n ($ns) { print $n; } function iterators user data structure + function used to navigate the structure the goto statement!!! Dijkstra s guarded if & do DFR - PL - Flow of Control 10
Control Flow Templates if E then S 1 if E then S 1 else S 2 while E do S 1 E.true E.false E.code* S 1.code E.true E.false S.next E.code* S 1.code goto S.next S 2.code S.begin E.true E.false E.code* S 1.code goto S.begin Note the distinction between the labels associated with the expression (E.true, E.false) and those associated with the statement (S.begin, S.next) *NB at end of E.code: if (E.code) goto E.true; go to E.false DFR - PL - Flow of Control 11
Control Flow summary Language Constructs sequence statements (incl Foc) simple / compound decision (1 / 2 / n way) if then [else] case / switch repetition for loop (counter) while (condition) * repeat (do) / until (cond) + Recursion 3AC: if X goto L / goto L Code generation if then (else); case; for; while; repeat; etc. are translated to if X goto L (conditional goto) goto L (unconditional goto) See templates above Operational semantics are used to describe foc constructs goto (break; exit; continue) may be language provided DFR - PL - Flow of Control 12