Site icon Dotnet Helpers

PowerShell : Do-While & Do-Until Loop

Do-While & Do-Until Loop in PowerShell

Loops are an essential construct in any programming language as they enable a block of code to be repeatedly executed based on the condition. This can be useful for iterating through items in an object or repeatedly performing an operation. PowerShell supports involves setting a condition which either allows the loop to process as long as the condition is true or until it is met. 

Do-While

Syntax : do {<statement list>} while (<condition>)

Example:

In below example, the loop will stop executing if condition became false that is if loop_index value reach 0 then condition became false.

Output:

Do-Until

Syntax : do {<statement list>} until (<condition>)

Example:

In below example, Do…Until loop is used when we want to repeat a set of statements as long as the condition is false.

Output:

 

Exit mobile version