Understanding break, continue, return, and exit

http://community.idera.com/powershell/powertips/b/tips/posts/understanding-break-continue-return-and-exit

 

o you know off-hand what "break", "continue", "return", and "exit" do? These are powerful language constructs, and here is a test function to illustrate how different their effects are:

'Starting'

function Test-Function {
    $fishtank = 1..10

    Foreach ($fish in $fishtank)
    {
        if ($fish -eq 7)
        {
            break      # <- abort loop   终止当前循环
            #continue  # <- skip just this iteration, but continue loop   终止此次循环的内容,进行下次唇环
            #return    # <- abort code, and continue in caller scope   跳出执行的代码,停留在当前的执行环境
            #exit      # <- abort code at caller scope             推出当前的执行环境
        }

        "fishing fish #$fish"

    }
    'Done.'
}

Test-Function


'Script done!'

Simply comment out one of the keywords and run the script to see how the loop behaves.

posted @ 2017-06-14 10:36  万寿宫主  阅读(178)  评论(0编辑  收藏  举报