Windows PowerShell 1.0 Looping with do and while Statements

From Techotopia
Revision as of 20:15, 27 October 2016 by Neil (Talk | contribs) (Text replacement - "<table border="0" cellspacing="0"> " to "<table border="0" cellspacing="0" width="100%">")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
PreviousTable of ContentsNext
Windows PowerShell 1.0 Looping with the for and foreach StatementsThe Windows PowerShell 1.0 switch Statement


Purchase and download the full PDF version of this PowerShell eBook for only $8.99


The Windows PowerShell for and foreach loops described in the chapter entitled Windows PowerShell 1.0 Looping with the for and foreach Statements works well when you know in advance how many times a particular task needs to be repeated in a program. There will, however, be instances where code needs to be repeated until a certain condition is met, with no way of knowing in advance how many repetitions are going to be needed to meet that criteria. To address this, Windows PowerShell provides the while and do ... while loops. Those familiar with other programming and scripting languages will find the Windows PowerShell implementations of these loops to be quite familiar.


Contents


The Windows PowerShell while Loop

Essentially, the while loop repeats a set of tasks until a specified condition is met. The while loop syntax is defined follows:


while (''condition'')
{
      # PowerShell statements go here
}

where condition is an expression that will return either true or false and the # Windows PowerShell statements go here comment represents the PowerShell code to be executed while the condition expression is true. For example:

$myCount = 0;

while ( $myCount -lt 100 )
{
      $myCount++
      "$myCount"
}

<google>ADSDAQBOX_FLOW</google> In the above example, the while expression will evaluate whether the $myCount variable is less than 100. If it is already greater than 100 the code in the braces is skipped and the loop exits without performing any tasks. This is essentially an "upfront" evaluation process. If, on the other hand, $myCount is not greater than 100 the code in the braces is executed and the loop returns to the while statement and repeats the evaluation of $myCount. This process repeats until the value of $myCount is greater than 100, at which point the loop exits.

Windows PowerShell do ... while loops

It is often helpful to think of the do ... while loop as an inverted while loop. The while loop evaluates an expression before executing the code contained in the body of the loop. If the expression evaluates to false on the first check then the code is not executed. The do .. while loop, on the other hand, is provided for situations where you know that the code contained in the body of the loop will always need to be executed at least once. For example, you may want to keep stepping through the items in an array until a specific item is found. You know that you have to at least check the first item in the array to have any hope of finding the entry you need. The syntax for the do ... while loop is as follows:

do
{
       // Windows PowerShell statements here
} while (''conditional expression'')

In the do ... while example below the loop will continue until the value of a variable named $i equals 0:

$i = 10;
do
{
       $i--
       "$i"
} while ($i -gt 0)

Breaking from while Loops

As with the for and foreach loop, it is also possible to exit from a while or do ... while loop at any time through the use of the break statement. When the execution path encounters a break statement, the looping will stop and execution will proceed to the statements immediately following the loop. In the following example, the loop is designed to exit when the value of variable $i matches the value of variable $j:

$i = 0;

$j = 5;

while ($i -lt 100)
{
     $i++

     if ($i -eq $j)
     {
           break
     }
     "$i"
}

It is important to note that in the case of nested loops the break statement only exits the current loop leaving the outer loop to continue executing (and most likely once again executing the inner loop). To break from all outer loops in a nested loop it is necessary to use a label as described in the chapter entitled Windows PowerShell 1.0 Looping with the for and foreach Statements.

The continue Statement

The continue statement causes all remaining PowerShell statements in a loop to be skipped, and execution to be returned to the top of the loop. In the following example, the write-host cmdlet is only called when the value of variable $j is an even number (i.e divisible by 2 with no remainder):


$j = 0;

while ($j -lt 20)
{
      $j++
      if (($j % 2) -ne 0)
      {
	   continue
      }

      write-host "j = $j"	
}

The continue statement in the above example will cause the write-host call to be skipped unless the value of variable $j can be divided by 2 with no remainder. If the continue statement is triggered, execution will skip to the top of the while loop and the statements in the body of the loop will be repeated (until the value of $j exceeds 19), resulting in the following output:

j = 2
j = 4
j = 6
j = 8
j = 10
j = 12
j = 14
j = 16
j = 18
j = 20

<google>BUY_WPS_BOTTOM</google>