[ The while looping construct ] [ The for looping construct ] [ The break statement ]
The while and for statements provide looping control in ALADDIN's problem solving procedures. The break statement forces an early exit from a looping construct.
Syntax : The while statement has the syntax :
while ( boolean expression ) { statement 1 ; statement 2 ; ...... statement N ; }
The statements 1 through N will be executed repeatedly while the boolean expression evaluates to "true."
Example 1 : The script
while ( 1 ) { print " x = ", x ,"\n"; }
contains an infinite loop that generates the output
x = 1 ksi x = 1 ksi x = 1 ksi .... and on forever ....
Example 2 : Looping constructs are essential for the efficient generation of finite element meshes. Suppose, for example, that we need to generate a line of nodal coordinates. The script
x = 0 ft; while( x <= 10 ft ) { print " x coord = ", x , "\n"; x = x + 2 ft; }
generates the output
x coord = 0 ft x coord = 2 ft x coord = 4 ft x coord = 6 ft x coord = 8 ft x coord = 10 ft
Example 3 : The coordinates in a two-dimensional finite element mesh can be generated with nested while loops. For example, the script:
node = 0; y = 0 ft; while( y <= 9 ft ) { x = 0 ft; while( x <= 6 ft ) { print " coord ", node, "[x,y] = [", x, y, "]\n"; node = node + 1; x = x + 2 ft; } y = y + 3 ft; }
generates 15 lines of output. An abbreviated summary is:
coord 0 [x,y] = [ 0 ft 0 ft ] coord 1 [x,y] = [ 2 ft 0 ft ] coord 2 [x,y] = [ 4 ft 0 ft ] coord 3 [x,y] = [ 6 ft 0 ft ] coord 4 [x,y] = [ 0 ft 3 ft ] ..... lines of output removed ..... coord 14 [x,y] = [ 4 ft 9 ft ] coord 15 [x,y] = [ 6 ft 9 ft ]
Syntax : The for statement has the syntax :
for ( initial statements; boolean expression; increment statements ) { statement 1 ; statement 2 ; ...... statement N ; }
The four steps of for-loop operation are:
Example 4 : The script of code
for( x = 0 ft; x <= 9 ft; x = x + 3 ft ) { print "x = ", x, "\n"; }generates the output
x = 0 ft x = 3 ft x = 6 ft x = 9 ftand demonstrates behavior in a basic for loop construct.
Example 5 : The initial and increment statement blocks may contain multiple statements, separated by commas. In the script
for( x = 0 ft, y = 0 ft; x <= 9 ft && y <= 4 ft; x = x + 3 ft, y = y + 2ft) { print "x = ", x, ": y = ", y, "\n"; }the initialization statements are
x = 0 ft, y = 0 ft;and the increment statement are
x = x + 3 ft, y = y + 2ft;The generated output is
x = 0 ft : y = 0 ft x = 3 ft : y = 2 ft x = 6 ft : y = 4 ftThe looping construct terminates when y is incremented to 6 ft and the relational operation to y <= 6 ft evaluates to false.
Example 6 : The initialization and increment statement blocks may be empty. For example, the script of code
x = 0 ft; y = 0 ft; for( ; x <= 9 ft && y <= 4 ft; ) { print "x = ", x, ": y = ", y, "\n"; x = x + 3 ft; y = y + 2 ft; }generates the same block of output as in Example 5.
The break statement forces an early exit from one layer of looping constructs. We will demonstrates its behavior via a series of examples.
Example 6 : The script of statements:
for( x = 0 ft ; x <= 9 ft ; x = x + 3 ft ) { print "x = ", x, "\n"; if (x == 6 ft) break; } print "breaking from loop \n"; print "x = ", x, "\n";generates the same block of output
x = 0 ft x = 3 ft x = 6 ft breaking from loop x = 6 ftand shows how the program execution exits the looping construct before the boolean expression has an opportunity to evaluate to false
Example 7 : What about 2 levels of looping construct ? In the script of statements:
for( x = 0 ft ; x <= 6 ft ; x = x + 3 ft ) { for( y = 0 ft ; y <= 9 ft ; y = y + 3 ft ) { print "x = ", x, "y = ", y, "\n"; if (y == 6 ft) break; } } print "finished looping construct\n"; print "x = ", x, "\n";the output is
x = 0 ft y = 0 ft x = 0 ft y = 3 ft x = 0 ft y = 6 ft x = 3 ft y = 0 ft x = 3 ft y = 3 ft x = 3 ft y = 6 ft x = 6 ft y = 0 ft x = 6 ft y = 3 ft x = 6 ft y = 6 ft finished looping construct x = 9 ftYou should notice how the values of y in the inner for loop never reach 9 ft because the relational operation
y == 6 ftalways evaluates to "true" beforehand. The break statement forces the program execution to exit one level of looing constructs.