control structures sequence of execution of high-level statements

25
Control Structures sequence of execution of high- level statements

Upload: roland-richard

Post on 04-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Control Structures sequence of execution of high-level statements

Control Structures

sequence of execution of high-level statements

Page 2: Control Structures sequence of execution of high-level statements

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

Page 3: Control Structures sequence of execution of high-level statements

Control by branching - FORTRAN

IF(X .EQ. Y) X=X+1

IF(X .EQ. Y) GO TO 100

<< ‘else’ statements >>

GO TO 200

100 CONTINUE (dummy statement)

<< ‘then’ statements >>

200 CONTINUE

<< rest of program >>

Page 4: Control Structures sequence of execution of high-level statements

Control by branching - FORTRAN

IF(X-Y) 10,10,20

10 CONTINUE

<< statements for X<=Y

GO TO 30

20 CONTINUE

<< statements for X>Y >>

30 CONTINUE

<< rest of program >>

Page 5: Control Structures sequence of execution of high-level statements

Statement labels

integers: FORTRAN, Pascal identifiers as constants: ALGOL, C variables! PL/1

Page 6: Control Structures sequence of execution of high-level statements

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

Page 7: Control Structures sequence of execution of high-level statements

Top-down structured programming 1975

flow chart model: no goto3 flow structures-sequence-if-then-else (selection)-do-while (iteration)

boolean expr

statement

Page 8: Control Structures sequence of execution of high-level statements

Selection: either / or how many alternatives:

1 do something or nothing 2 if then else 3 IF (X-Y) 10,20,30 many switch, nested IF, COND (LISP)

variations on the selection structure

Page 9: Control Structures sequence of execution of high-level statements

Selection design decisions - 1

control of multiple statements in the control statement

if (condition)then

<statements>else

<statements>endif

compound statement / block

Page 10: Control Structures sequence of execution of high-level statements

Selection design decisions - 1

control of multiple statements in the control statement

compound statement / blockif (condition) if (condition)

<statement> { <statements>else }<statement> else

{ <statements>

}

Page 11: Control Structures sequence of execution of high-level statements

Selection design decisions - 2 nesting selection – syntax and logic

syntax – use block or control structureif (sum==0) if (count==0) then result = 0; endif;else result = 1;endif;

convention/rule

Page 12: Control Structures sequence of execution of high-level statements

Selection design decisions - 2

nesting selection – syntax and logic syntax – use block or control structure convention/rule java, etc

if (sum==0) //p.347 ? if (count==0)

result = 0;

else

result = 1;

Page 13: Control Structures sequence of execution of high-level statements

Multiple selection

many variations1. FORTRAN computed goto is multiple branch

GO TO (10,20,30,40,50,60), X^^2+Y

2. case / switch model (independent selections)• design decisions

• default case allowed/ignored?• branch after section

3. nested ifs (dependent selections)4. guarded statements

Page 14: Control Structures sequence of execution of high-level statements

Multiple selection - 1FORTRAN computed goto is multiple branch

GO TO (10,20,30,40,50,60), X^^2+Ylabel list computed index

semantics:compute indexif index < 1 or > max(6), go to next statementgo to label at index location in label list

Page 15: Control Structures sequence of execution of high-level statements

Multiple selection - 2

switch/case model

Page 16: Control Structures sequence of execution of high-level statements

Multiple selection - 3 using if

Ada:if (condition)

<stmt>

elsif (condition)

<stmt>

elsif (condition)

<stmt>

else

<stmt>

LISP(COND ((condition)(value))

((condition)(value)) ((condition)(value)) ( T

(value)))

4

1

1

1 2

2

2

3

3

3

4

4

Page 17: Control Structures sequence of execution of high-level statements

Multiple selection - 4 guarded statements – Dijkstra

evaluate all conditions: none true – error one true – do statement many true – select statement at random

if <condition> -> <statement>[] <condition> -> <statement>[] <condition> -> <statement>[] <condition> -> <statement>fi

Page 18: Control Structures sequence of execution of high-level statements

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

Page 19: Control Structures sequence of execution of high-level statements

Counter controlled loops developed with array indexing counter (loop variable) parameters

start, stop, stepsize

Page 20: Control Structures sequence of execution of high-level statements

Counter controlled loops design decision

parameters computed whenonce or every execution?

x = 10; do I = 0 to x x = x - 111 executions or 6 executions

Page 21: Control Structures sequence of execution of high-level statements

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;

Page 22: Control Structures sequence of execution of high-level statements

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 << body >>100 CONTINUE

Page 23: Control Structures sequence of execution of high-level statements

Generalizing the counter start, stop, step - FORTRAN sequence, index expression & condition,

“all of the above” - ALGOLfor i = 4,5,6 i*10 while i,10000, 12, 14 step

10 until 25 do

simple counting – Ada, Pascal expressions – C,C++,javafor (<expr1>,<expr2>,<expr3>)

<statement>

Page 24: Control Structures sequence of execution of high-level statements

Logical control design issue: pretest or posttest? most languages have ‘while’ pretest loop posttest:

Pascal C sum := 0; sum = 0;

repeat do

sum := sum + 15 sum += 15;

until (sum > 100); while (sum <= 100);

Page 25: Control Structures sequence of execution of high-level statements

Constrained branching

break switch statement 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