The combination of IF and GOTO statements can handle all types of control structure requirements.
IF (X .GT. 0) GOTO 10 ... Statements for X <= 0 ... GOTO 20 10 CONTINUE ... Statements for X> 0 ... 20 CONTINUE |
C Loop Entry 10 IF (X .GT. 0) GOTO 20 ... WHILE X <= 0 loop ... GOTO 10 20 CONTINUE |
| IF-THEN-ELSE in FORTRAN | WHILE in FORTRAN |
|---|
In 1968, Edsger W. Dijkstra advocated abolition of the GOTO statement in a classic paper entitled Go To Statement Considered Harmful (Communications of the ACM Vol. 11, No. 3, March 1968, pp. 147-148.
Many programming languages use a simple if-then control structure with an optional else part.
then <statement> [ else <statement> ]This is an ambiguous syntax that gives rise to the dangling else problem as illustrated by the following example.
if E1 then if E2 then S1 else S2
Here, else S2 is a dangling clause that could be associated
with either the inner or the outer if-statement.
Most programming languages resolve this by associating the else part with the innermost structure. A rather more complex grammar is needed to describe this, see for example, Section 14.9 of the Java Reference Manual.
Matching the innermost structure might not always be in accord with the programmer intent, though.
if E1 then if E2 then S1 else S2
In this example, the programmer probably intended that the dangling else part should be associated with the outermost if-statement. Note, languages employing the offside rule would interpret this correctly.
Many languages use a multiway if structure to avoid the dangling-else problem
as well as addressing two other common concerns. The first of these is
that then and else branches often require more than one statement.
The second is that logic chains often generate deeply-nested else parts.
The following example illustrates these concerns in Pascal syntax.
if E1 then begin S1a; S1b end else if E2 then begin S2a; S2b end else if E3 then S3 else if E4 then S4 else begin S5a; S5b; end;
The multiway if addresses all these concerns. The Ada syntax
is typical.
if condition then
<statement-list>elsif condition then
<statement-list>}else
<statement-list>]end if;if E1 then S1a; S1b; elsif E2 then S2a; S2b; elsif E3 then S3; elsif E4 then S4; else S5a; S5b; end if;
In Python (see section 7.1 of the reference manual), the offside rule is used to avoid the need for an ending keyword.
if E1: S1a; S1b elif E2: S2a S2b elif E3: S3 elif E4: S4 else S5a S5b S6
In this example, both S5a and S5b belong to the
else part, but S6 does not.
The C/C++/Java switch statements and
the Pascal/Modula/Ada case statements
provide multiway branching based on the value
of a particular expression.
Generally, case or switch statements can be more
efficiently implemented than a linear series of
tests in a multiway if statement.
For example, a jump table can be used for selection
of cases based on contiguous values.
break
statement. Although omitting a break has
occasional use, the design is error prone.
Condition-based iteration refers to loops whose control (i.e., loop continuation or loop termination) is based on evaluation of boolean conditions.
The most common forms are the single-entry, single-exit loops with a test occurring either at the beginning of the loop (while loop) or at the end (do-while loop).
Indexed iteration refers to loops whose control is based on iterating through a specified set of index values.
Most commonly, indexed iteration is achieved using a
for loop ranging over a specified set of
integer values.