Do You Use "Break"?

by Oct 13, 2010

"Break" is a special keyword that you can use to exit loops and conditions prematurely. Have a look:

Do {
$pwd = Read-Host 'Enter your password'
If ($pwd -eq 'topsecret') { break }
} While ($true)

As such, the loop would run endless. Its exit condition is an embedded condition that calls break when appropriate. Of course, you could have placed your exit condition after While as well, but break allows you to be more flexible and have multiple exit conditions at various code positions.

Twitter This Tip!
ReTweet this Tip!