Advertisements

An Introduction to PowerShell – Do While

Do While is the simplest of the looping constructs in PowerShell.

A looping construct is basically a piece of code that repeats the same action over and over again to a set of iteration, it loops through a set of statements, doing some action in each of its iteration, until some condition is met or that set of statement is exhausted.

Do While is simply a construct that says to PowerShell, “repeat set of things until some condition becomes true”.

For example, let’s set up a control variable called count and give it an initial value of one.

$count = 1

Then, let’s set up a simple Do While construct that adds 1 to whatever value of count is already in that variable, until the variable has the number 10 in it.

Do

{

$count = $count + 1

Write-Host “The current value of the variable is $count”

} While ($count –lt 10)

 

PS-10

Another way of doing same is:

You can also set up a Do While construct so that your set of commands only executes when the condition is true. You just need to eliminate the do statement, and only use while.

While ($count –lt 10)

{

$count = $count + 1

Write-Host “The current value of the variable is $count”

}

PS-11

Only difference in above both way of using Do While construct is in first way if we initialize Count with 11 then also the loop with execute once as the condition is tested after loop is executed at least once if condition is satisfied the loop will continue else will exit. Where as in second way the condition is tested before it enters the loop and will exit before first iteration of the loop.

PS-12

I will come up with more stuffs in my upcoming posts. 

Till then keep practicing and stay tuned for more details.

 

 

Advertisements

Comments are closed.

Important

Most of the contents you find in this blog will be either inherited from MSDN or Navision Developer IT Pro Help. Some places images are also directly taken from these sites. Purpose is simple to try those stuffs and re-produce adding few things as per my understanding to make easy understanding for others and quick reference.

Here nothing under my own brand or authorship of the content. At any point of time we are just promoting Microsoft stuffs nothing personnel with same.

Hope stuffs used here will not violate any copyright agreement with them. In case by mistake or in-intestinally it happens and the Microsoft feels these should not be used Microsoft have full right to inform me about same and will be glad to take down any such content which may be violating the norms.

Purpose is to promote Navision and share with community.

FB Profile

Like FB Page

%d bloggers like this: