Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Labeled break statements in PowerShell

$
0
0

What is the difference between including a ':' (colon) in a labeled break statement and not including the colon?  The documentation seems to indicate that a colon is necessary but it seems to work the way I would expect only without a colon.

More specifically without a colon the following script works as I would expect

:test1 for ($ i= 1; $i -le 2; $i++) {
          for ($j = 1; $j -le 2; $j++) {
                  for ($k = 1; $k -le 2; $k++) {
                             Write-Host "$i, $j, $k "
                              if ($i -eq 2) {
                                  break test1
                              }
                     }
            }
}

and outputs

1, 1, 1 
1, 1, 2 
1, 2, 1 
1, 2, 2 
2, 1, 1 

But if I add a colon to the labeled break statement (change line 6 to break :test1) the script outputs 

1, 1, 1 
1, 1, 2 
1, 2, 1 
1, 2, 2 
2, 1, 1 
2, 2, 1 

which I wouldn't expect.  What is the difference between the scripts with and without a colon in the break statement?

Thanks, David




Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>