[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
FOR
The for statement is used to loop continuously while a certain
condition is true. The advantages over the while statement is that a
count variable can be initialized and incremented quite easily. The
for statement has the form:
for (<expression1>; <expression2>; <expression3>)
<statement>
The first expression is the one that should initialize the count
variable. For example, if you wanted to count from 1 to 100, and
were keeping the count in a variable called 'num', the first ex-
pression would be 'num = 1'. The second expression is the condi-
tional test. As long as it evaluates to non-zero (TRUE), the state-
ment will be executed. Following the above example, this expression
would be 'num < 100'. The third expression is the one that is used
to increment the count variable. For the above example, it would
therefore be 'num = num + 1'. This for statement differs in format
from that in most other languages, but doing it this way is actually
gives the programmer a lot of power and flexibility. Note that any
of the expressions can be left empty, in which case they evaluate to
non-zero (TRUE). Some examples are:
for (count = 0; count < 100; count = count + 1)
{
printn(count);
prints("");
}
for (c = 1000; c > 0; c = c - 1)
do_this(c);
The following would execute an infinite loop:
for (;;)
prints("Hello!");
Note that there is really no restriction on what the expressions
are. For example, the following is quite legal:
for (c = num = 0; c < 100 && stat != -1; c = c + 1)
{
stat = func(num);
num = func2();
}
The statements would only be executed if c was smaller than 100 and
stat didn't equal -1.
See Also:
break
continue
do
while
expressions
operators
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson