Loops - I Did It Again
Loops are the part of your code that let you perform a set of actions for some number of iterations (an iteration is doing everything in the loop once without repeating). While you can write a script without loops, you can condense your code and make things a lot simpler.
Loops are the part of your code that let you perform a set of actions for some number of iterations (an iteration is doing everything in the loop once without repeating). Loops are all about patterns and repeatability, so when you notice something in your code happening over and over again you can look for ways you can use a loop to improve your code. Loops repeat over and over until a defined condition is met. The types of loops available in Powershell are while, do, for, and foreach.
Each loop is a little different but has the same construction of a loop type, a condition, and a code block (except the foreach, which doesn't have a condition, you'll see why later).
While
A while loop iterates over a piece of code while a condition is true. If that condition isn't true when the while loop first evaluates it, the while loop will not run.
Note the $Count++ at the end of the code block. If you don't increment the $Count variable your loop will never stop running. This is called an Infinite Loop.
Infinite Loop
Infinite loops are loops that will run forever without interruption. The only way to stop them is to "pull the plug" and kill the process running or reboot the machine. Be careful to not do this unintentionally (there are some use cases for an infinite loop, many cartridge games used them as there was no OS for the game to exit to and instead exited when the device was turned off).
Do While/Until
There are two forms of the Do loop in Powershell, Do While, and Do Until. Unlike the while loop, a Do loop will always run once, and then check the condition (Do loops check the condition at the end of an iteration, while a while loop checks at the start of the iteration). A do while loop will continue running while the condition is true, and a do until loop will continue running until the condition is true.
Note that the do until loop will run until the variable $Count equals 0. In this case the code is simpler and we know that the condition will be met, but with more complicated code it can be better to use a more general condition to make sure it will exit. Using $Count -le 0 would have the same affect in this case, and prevent the loop from continuing on forever if it somehow were to skip over 0.
For
With while and do loops we have to declare our iterator (the $Count variable in the above examples) and handle changing it inside the code block. A for loop declare it's own iterator, and how to handle changing it on each iteration, along with the condition that is being checked. To declare a for loop it follows the format of for(iterator declaration; condition to check; iterator handling)
Off-by-one Errors
Like array indexes iterated loops are a common spot for an off-by-one error. Starting your for loop on zero, when you really wanted to start with one, or using a -lt when you really need a -le because you need to iterate over the 20th object, and not just over the 19th, is a common mistake even among practiced programmers. It always pays to slow down and carefully read over your loops to make sure that you haven't inadvertently created an off-by-one error.
Foreach
Foreach loops are used to iterate over each object in a set (such as an array). This can be really useful as you don't need to know how many objects are in the set as you would with any of the previous loops. A foreach loop is declared with the format foreach(item in ObjectSet)
Break and Continue
Sometimes you will want to stop iterating through a loop early. In this case you can use either the break or continue statement to exit the loop. The break statement will exit the loop entirely and go on to the next bit of code in the script while the continue statement will stop processing the iteration that the loop is on and go to the next iteration. Some examples of how you would use each are below.
Once you've found the needle in the haystack there is no need to keep looking, so you can break out of the loop and continue on to what you need to do next.
Since we want to keep going through the loop, but don't want to do anything with the even numbers we can use a continue to skip the rest of the current iteration and go on.
Back to Tic-Tac-Toe
We used the example of Tic-Tac-Toe while talking about Arrays, and we can expand that example a little now with a loop.
Now we have a loop to play through the game. It can only play 9 turns (because that would fill the board) and has to check with the user to see if someone won. We'll expand the example further in the functions section.