do loop

The do loop has two possible conditions, until and while, which are validated after the first execution, at the end of the do loop. Since the condition is only checked after the script block, the script block is executed at least once regardless of how the condition is evaluated.

The do loop syntax, with the two possible conditions, looks as follows:

# do-loop with while condition - simple syntax
# do
# {
# <statement list>
# }
# while (<condition>)

# do-loop with until condition - simple syntax
# do
# {
# <statement list>
# }
# until (<condition>)

The first loop will be executed as long as the conditional expression after the while keyword returns $true, while the second loop is only executed as long as the conditional expression after the until keyword returns $false. The following two examples will create the same result. Take a look at them, and try to understand how they work by going through them step by step:

# simple example for do-while loop
$a = 0
do
{
$a++
$a
}
while ($a -lt 5)
# 1, 2, 3, 4, 5

# simple example for do-until loop
$a = 0
do
{
$a++
$a
}
until ($a -gt 4)
# 1, 2, 3, 4, 5
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.150.163