Databases Reference
In-Depth Information
❑
Do-Until
loop
❑
foreach
statement
For Loop
For
loop is one of the most commonly used loops in programming languages. This
for
loop construct is
similar to C language and has three parts:
1.
init-expr
, if it exists, is executed. Typically this initializes one or more counters, and may
also declare them as well.
2.
eval-expr
evaluates the current condition. If it is true, then it continues the loop.
3.
increment-expr
is executed if it exists. Typically this increases or increments by one or more
counters.
The next example, shown in Figure 3-15, illustrates the multiplication table for 5. Basically, it starts the
loop with value 0, evaluates the value, and determines whether it is less than 11. The loop continues until
the condition is true and increases the value by 1 each time it loops:
$myvar
=
5
for ($i
=
0; $i -lt 11; $i
++
)
{write-host $i "X" $myvar "
=
" ($myvar*$i)}
Figure 3-15
While Loop
The same multiplication table can be written using a
while
loop statement. The difference between a
for
loop and a
while
loop is that the value is initialized before the
while
loop. With the
while
loop construct,
the evaluation and the incremental portion of the loop is written inside the loop as an expression. Here
the looping continues until the condition is true (see Figure 3-16):
$myvar
=
5
$i
=
0
while ($i -le 10)
{ write-host $i "X" $myvar "
=
" ($myvar*$i) ;$i
++
}
Search WWH ::
Custom Search