HTML and CSS Reference
In-Depth Information
EXPLANATION
1
The variable
i
is initialized to
0
.
2
The
do
block is entered. This block of statements will be executed before the
while
expression is tested. Even if the
while
expression proves to be false, this block will
be executed the first time around.
3
The value of
i
is displayed in the browser window (see Figure 6.5).
4
The value of
i
is incremented by 1.
5
Now, the
while
expression is tested to see if it evaluates to true (i.e., is
i
less than
10
?). If so, control goes back to line 2 and the block is re-entered.
sa
Figure 6.5
Output from Example 6.6, the
do/while
loop.
The
for
loop consists of the
for
keyword followed by three expressions separated by semico-
lons and enclosed within parentheses. Any or all of the expressions can be omitted, but the
two semicolons cannot. The first expression is used to set the initial value of variables and
is executed just once, the second expression is used to test whether the loop should con-
tinue or stop, and the third expression updates the loop variables; that is, it increments or
decrements a counter, which will usually determine how many times the loop is repeated.
FORMAT
for(Expression1;Expression2;Expression3)
{statement(s);}
for (initialize; test; increment/decrement)
{statement(s);}
The preceding format is equivalent to the following
while
statement:
Expression1;
while( Expression2 )
{ Block; Expression3};