CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Understanding the While loops

PowerShell provides loops to execute the sequence of statements repeatedly. These loops executes the block of statements within it, till a given condition is satisfied.

Through this article, we go through while loop and do loops; do…while and do…until loops.

While loop

PowerShell While loop is to execute the statements within it continuously, if the given condition is satisfied. It allows to enter to the loop; only when the given condition is True. If the condition is not satisfied, it returns out from the loop.

Let’s take a simple example, where it loop through the array and display it’s elements;

$arr = (1, 2, 3) 
$idx = 0

# while loop demo
#
while ($idx - lt $arr.count)
{
  echo $arr[$idx++]
}

Observe that, it is important to set the variable $idx value to “0” (array index starts from “0”); to satisfy the condition in while loop to allow to enter into it. I recommend you to read, “PowerShell – How to use Equality operators” to know more about Equality operators (-lt, -gt, -eq etc,.).

The condition in while loop verifies, whether the value of $idx is lesser than the value of array count. As this ($idx) is index value, it must be with in the boundaries of the array. When it crossed, the while loop exits. It is important to increment the index value ($idx) by 1 to point to next index of an array. Hence, the $idx++ within the loop. Otherwise, the loop executes infinitely.

DoWhile loop

Other variation of while statement is DoWhile statement. The difference is, DoWhile statement executes at least once. Because, the condition to continue the loop will be at end; where as in While statement the condition to continue the loop is at the beginning.

Let’s take another example, where it navigate through the string and display each character from the string;

# do...while loop, navigate within the string
#
$str = "The Avengers Movie!"
$idx = 0

do
{
	echo $str[$idx++]
}
while ($idx -lt $str.length)

Observe that, the condition comes at end of the loop; which verifies whether the index value ($idx) is within the boundary ($str.length – which gives length of the string).

DoUntil loop

Another variation of Do loop is; DoUntil loop. This loop will execute the statements within it, until the condition is true. Read it carefully. Did you follow it? Above two loops will execute the statement, when the condition is met; whereas this loop executes the statements when the condition is False.

Let’s take an example, to demonstrate this loop; this example, loop through the array and if it finds any string, it loop through the string to display each character;

# do...until loop, navigate into array of arrays
#
$arr_arr = (1234, "Matrix", (2.3, 8.9))
$idx = 0

do
{
	if ( $arr_arr[$idx].GetType().Name -eq "String" )
	{
		$idx2 = 0
		
		echo "---"
		do
		{
			echo $arr_arr[$idx][$idx2++]
		}
		until ($idx2 -ge $arr_arr[$idx].length)
		echo "---"

		$idx++
	}
	else
	{
		echo $arr_arr[$idx++]
	}
}
until ($idx -ge $arr_arr.count)

Above example, uses a loop within a loop. Yes, it is possible for all these loops; to use a loop within another loop.

In the loop, GetType().Name will return the type of the element and the if condition verifies whether it is a String; then, it starts a new loop to traverse within the loop. Be cautious, when you use loops within the loops.

Based on your Program requirement you can use these loops to fulfill your program need.

[..] David

PowerShell – Understanding the While loops

2 thoughts on “PowerShell – Understanding the While loops

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top