Break,Continue and Labeling Loops

Today I want to write about thing which literally blowed my mind when I learned about it. I am not sure how long it been around (maybe even from Powershell v1) but I find out about it recently. It is ability to assign label to a loop construct and use break or continue with the label.

But lets not get ahead of oneselfs and start from the beginning.

Powershell has wide variety of loop constructs: for, foreach, while, do-while, do-until etc.

Break is special statement which causes PowerShell to immediately exit loop. Continue statement, in its turn, immediately returns the program flow to the top of the innermost loop.

Basically if you need to exit loop when some condition is met you use break and when you need to go to next iteration after some condition right away you use continue.

Some examples:

$Collection=10..16
Write-Output "Collection: $Collection"
$i=0
foreach ($item in $collection)
{
  if($item -eq 13){
    break
  }
  $i++   
}
Write-Output "Index of 13 is $i"
Break Example

It this example when $item is equal to 13 we immediately exit loop and it doesnt matter that we did not loop through all of the Collection items.

$Collection=10..16
Write-Output "Collection: $Collection"
Write-Output "Collection without 13:"
foreach ($item in $collection)
{
  if($item -eq 13){
    continue
  }
  Write-Output "$item"
}
Continue Example

In our continue example we are looping through same collection and outputting all items except number 13.

Loop Labeling

Now the most interesting part Labeling loop. Let’s dive right into it.

In Powershell you can assign label to a loop construct and then specify from which loop break/continue by using that label. This is very powerful and useful with nested loops.

Let’s look at the following example:

:Condition1Loop while (<condition1>)
        {
            :Condition2Loop while (<condition2>)
            {
		:Condition3Loop while (<condition3>)
                {
                    while(<condition4>)
		    {
			if ($variable -eq $a) {break}
                    	if ($variable -eq $b) {continue Condition2Loop}
                    	if ($variable -eq $c) {break Condition1Loop}	
		    }
		    # Code after "Condition4Loop"
                }
                # Code after "Condition3Loop" 
            }
            # Code after "Condition2Loop"
        }
        # Code after "Condition1Loop"

It the code snippet mentioned above if $variable is equal to value $b we will follow continue to the top of the Condition2 loop.

If $variable is equal to $c execution of the script will resume after Condition1Loop. If our $variable is equal to value $a we will break out of the inner loop with condition4 no label is required.

Windows PowerShell does not limit how far labels can resume execution. The label can even pass control across script and function call boundaries.

Thanks a lot for reading.
If you have any questions please leave them in a comment section below.

2 thoughts on “Break,Continue and Labeling Loops”

  1. Dude your ads take up 80% of the screen you desperately need to cut that down.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.