Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures sequence of execution of high-level statements.

Similar presentations


Presentation on theme: "Control Structures sequence of execution of high-level statements."— Presentation transcript:

1 Control Structures sequence of execution of high-level statements

2 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

3 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

4 Control by branching - FORTRAN IF(X.EQ. Y) X=X+1 IF(X.EQ. Y) GO TO 100 > GO TO 200 100 CONTINUE (dummy statement) > 200 CONTINUE >

5 Control by branching - FORTRAN IF(X-Y) 10,10,20 10 CONTINUE << statements for X<=Y GO TO 30 20 CONTINUE Y >> 30 CONTINUE >

6 Statement labels  integers: FORTRAN, Pascal  identifiers as constants: ALGOL, C  variables! PL/1

7 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

8 Top-down structured programming 1975 flow chart model: no goto 3 flow structures -sequence -if-then-else (selection) -do-while (iteration) boolean expr statement

9 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

10 Selection design decisions  control of multiple statements in the control statement compound statement / block

11 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;

12 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?

13 Multiple selection (2)  guarded statements – Dijkstra evaluate all conditions:  none true – error  one true – do statement  many true – select statement at random if -> [] -> fi

14 Multiple selection (2)  Ada: if (condition) elsif (condition) elsif (condition) else  LISP (COND (( condition )(value)) (( condition )(value)) (( condition )(value)) ( T(value))) 4 1 1 1 2 2 2 3 3 3 4 4

15 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

16 Counter controlled loops  developed with array indexing  counter (loop variable) parameters start, stop, stepsize

17 Counter controlled loops  design decision parameters computed when once or every execution ? x = 10; do I = 0 to x x = x - 1 11 executions or 6 executions

18 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;

19 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

20 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 (,, )

21 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);

22 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


Download ppt "Control Structures sequence of execution of high-level statements."

Similar presentations


Ads by Google