Presentation is loading. Please wait.

Presentation is loading. Please wait.

Statement Level Flow of Control GOTOs and Other Weirdness Copyright © 2003-2014 by Curt Hill.

Similar presentations


Presentation on theme: "Statement Level Flow of Control GOTOs and Other Weirdness Copyright © 2003-2014 by Curt Hill."— Presentation transcript:

1 Statement Level Flow of Control GOTOs and Other Weirdness Copyright © 2003-2014 by Curt Hill

2 Unconditional Branching This is an inheritance from machine language through FORTRAN and other 1960s imperative languages The controversy has already been discussed The problem is: How did I get here? We always have this problem with flow of control: –Through the then or the else? –Did the leading decision loop execute zero times or was the last statement in the loop Copyright © 2003-2014 by Curt Hill

3 How did I get here? In the case of decisions and loops you can form a predicate that describes how you got there –With a loop you also know that you could not arrive unless the loop exit condition was true In the case of goto this predicate is substantially more complicated Copyright © 2003-2014 by Curt Hill

4 Issues Form of the label? –Does it need declaration? Location of the target? –Usually in this unit or elsewhere? Copyright © 2003-2014 by Curt Hill

5 Form of labels FORTRAN, Pascal use unsigned integers –Pascal declares them in advance to discourage their use ALGOL 60, C, C++ use identifier forms Ada brackets labels > –Makes the label easy to see Copyright © 2003-2014 by Curt Hill

6 Two Examples Copyright © 2003-2014 by Curt Hill Ada goto target; … > x:=x+1; Pascal Label 4; { In declarations } … goto 4; … 4: x:=x+1;

7 Others COBOL can change the destination with an Alter statement GOTO can be nullified or change destination How does this affect readability? PL/I allows them to be variables so that the value can be assigned –The label to be branched to may change during the course of execution Copyright © 2003-2014 by Curt Hill

8 PL/I Example 1 Copyright © 2003-2014 by Curt Hill PL/I simple goto and label goto L1; L1: x = x+1;... L2: z = z*2;...

9 PL/I Example 2 Copyright © 2003-2014 by Curt Hill The not so simple: DECLARE LX LABEL; DECLARE L_AR(5) LABEL; … LX = L2; … GOTO LX; … GOTO L_AR(K); … L2: z = z*2;

10 Restrictions on gotos Can you branch into an subroutine or only out of it? Pascal allows you to branch out of a subroutine into its calling ancestor FORTRAN did allow a branch out of routine and into another one Copyright © 2003-2014 by Curt Hill

11 Typical Use In the modern times the GOTO is mainly an error exit Pascal is typical –We use it to bail from deeply nested constructs, including subroutines –Many implementations of Pascal and other languages force the GOTO to stay local Error handling has largely supplanted the need for the GOTO Copyright © 2003-2014 by Curt Hill

12 Guarded commands Developed in the context of proofs of correctness –Started by Dijkstra –Most of the work has been done in the Artificial Intelligence area Motivation: If the order of evaluation is not important the program should not force one Copyright © 2003-2014 by Curt Hill

13 Guarded IF The if has several arms –Boolean followed statement –Of the true booleans any or all could be picked –Non-deterministic Similar to the LISP COND Copyright © 2003-2014 by Curt Hill

14 Guarded if Form: if [ ] a>b -> c = a [ ] a=b -> c = a [ ] a c = b fi There could be as many booleans and statements as you would like One that was true would be non- deterministically chosen executed Copyright © 2003-2014 by Curt Hill

15 Guarded Loop There was also a loop construct The loop could not exit as long as one Boolean was true: do [ ] q1 > q2  swap(q1,q2); [ ] q2 > q3  swap(q2,q3); [ ] q3 > q4  swap(q3,q4); od Copyright © 2003-2014 by Curt Hill

16 Implementations Scarce Computers are inherently deterministic machines –Extra work to make sequential constructs non-deterministic Gained little popularity in sequential languages –More in concurrent languages Copyright © 2003-2014 by Curt Hill

17 Guarded Again There have been numerous proposals –None have caught on The idea is that we can mathematically develop programs that are provably correct –The proof is generated as a side effect of the writing the program Copyright © 2003-2014 by Curt Hill

18 Finally The structured programming school has dominated thinking in imperative languages concerning flow of control The GOTO has all but disappeared –This is why SNOBOL is so difficult The proof of correctness factions have made little impact Copyright © 2003-2014 by Curt Hill


Download ppt "Statement Level Flow of Control GOTOs and Other Weirdness Copyright © 2003-2014 by Curt Hill."

Similar presentations


Ads by Google