Limbo has three types of iteration statements that repeat execution of a statement or block of statements until a certain condition is reached: while, do, and for loops.
Each of the loop statements can be preceeded by a label that is used in conjunction with the jump statements break and continue. See Labels later in this section for more information.
for
loop in Limbo, like C and other languages, is flexible and powerful.
The general format of the for
loop is:
for (Inside the parentheses there are three expressions. These can be any valid Limbo expression. The following are the most commonly used expressions:exp1
;exp2
;exp3
)statement
;
exp1
is an assignment or initialization for an index variable
exp2
is the test for loop continuation
exp3
is modification to the index variable, such as increment or decrement, for each loop iteration
statement
can be an empty statement, a single statement, or a statement block enclosed in braces ({...}
).
For example, the following uses a for
loop to print the numbers 1 to 100:
i: int; for (i = 1; i <= 100; i++) sys->print("%d ", i);Notice the declaration of the integer variable,
i
, before entering the for
loop. The declaration and assignment (initialization) can also be done in the first expression of the for
statement:
for (i := 1; i <= 100; i++) sys->print("%d ", i);
for
statement are required. For example, you can create infinite loops, time delay loops, multiple control variable loops, and many others with the flexibility of the for
loop.
For example, the for
loop is the iteration statement conventionally used to create an infinite loop. The following example continuously reads one character from standard input until the user enters a carriage return (presses the Return or Enter key):
stdin := sys->fildes(0); #get FD for stdin buf := array[128] of byte; n : int; for(;;) { n = sys->read(stdin, buf, 1); #check for CR or ^D (break, n=0) if ((int(buf[0]) == 10) || (n <=0 )) break; }For information about
break
used to terminate the loop, see The break Statement on page 2-40.
while
loop is a general iteration statement. The general format of the while loop is:
while (If thecondition
)statement
;
condition
is true, then the statement
is executed. The statement
can be an empty statement, a single statement, or a statement block enclosed in braces ({...}
).
For example, the following program fragment prints 1 to 100:
n := 1; while (n <= 100) sys->print("%d ", n++);
for
and while
loops, the do
...while
loop tests the condition at the end of the iteration. This means that a do
...while
loop executes at least once.
The general form of the do
...while
loop is:
doThestatement
; while (condition
);
statement
can be an empty statement, a single statement, or a statement block enclosed in braces ({...}
). The statement
is repeated if the condition
is true.
For example, the following shows the do...while version of the counter:
n := 1; do { sys->print("%d ", n++); } while (n <= 100);Although the braces are not required when there is only one statement, they are generally used to improve readability.