Control Statements Affect Readability
Control statements provide:
-
Syntactic encapsulation
-
top-down reading of programs
Alternative to control statements is the use of GOTO statements
-
Forces reader to jump from statement to statement
-
Often backwards
-
Often to far-off locations
Spaghetti-Bowl problem
Sample comparisons: FORTRAN IV versus C++:
Logic-Controlled Pre-Test
Loop in FORTRAN IV :
100 IF (VAR .GE. 0) GOTO 200
...
GOTO 100
200 ...
Logic-Controlled Pre-Test Loop in C++:
while( var < 0 )
{
...
}
Logic-Controlled Post-Test Loop in FORTRAN IV :
100 ...
...
IF (VAR .GE. 0) GOTO 100
Logic-Controlled Post-Test Loop in C++:
do
{
...
} while( var >= 0 );
Counter-controlled loop in FORTRAN IV:
DO 100 I = 1, 10, 2
...
100 CONTINUE
Counter-controlled loop in C++:
for( i = 1; i <= 10; i = i + 2 )
{
...
}