Windows Powershell Do While loop _powershell

Source: Internet
Author: User

Conditions for continuing and terminating loops

Do-while () will be executed before you can make sure that the loop executes at least once.

Copy Code code as follows:

PS c:powershell> do {$n =read-host} while ($n-ne 0)
10
100
99
2012
The end of the world
Why don't you quit?
Because the condition is not satisfied
How to meet
Please enter a 0, try
0
PS c:powershell>

Use while in isolation

Copy Code code as follows:

$n =5
while ($n-GT 0)
{
$n
$n = $n-1
}
5
4
3
2
1

Terminate the current loop

Use the Continue keyword, but terminate the current loop, skip the other statements after continue, and recycle again.

Copy Code code as follows:

$n =1
while ($n-lt 6)
{
if ($n-eq 4)
{
$n = $n +1
Continue

}
Else
{
$n
}
$n = $n +1
}
1
2
3
5

Jump out of the loop statement

Use the break keyword to jump out of a loop statement

Copy Code code as follows:

$n =1
while ($n-lt 6)
{
if ($n-eq 4)
{
Break
}
$n
$n + +
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.