Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 6. Using Loops to Process Data > Altering Loop Execution

Altering Loop Execution

Sometimes certain conditions may occur in which you will want to prematurely terminate the execution of a loop. For example, if you wrote a foreach loop in order to search an array for a given element and you found that element somewhere in the middle of the array, rather than iterating through the rest of the array just for the fun of it, you’ll probably want to break out of the loop and get on with the business at hand. Alternatively, you may want to prematurely stop the current iteration of a loop without actually terminating the loop itself. Windows PowerShell supports both of these actions through the break and continue commands.

Using the break Command

When the break command is executed, the innermost loop is terminated and processing control jumps to the next statement that follows the end of the loop.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint