Break vs Continue vs Return vs Exit (PowerShell Example)



  • A little script to show the difference between them all, and the results. I thougth it was an fun experiment.

    Script:

    Write-Output "Starting script..."
    
    function Start-CountingSheep {
        $sheepfarm = 1..99
    
        Foreach ($sheep in $sheepfarm)
        {
            if ($sheep -match 9)
            {
                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 
            }
    
            "Counting sheep #$sheep"
    
        }
        'Done counting sheep!'
    }
    
    Start-CountingSheep
    
    Write-Output "...script finished."
    

    Break Output Example:
    (at the first sign of a nine, abort foreach loop)

    Starting script...
    Counting sheep #1
    Counting sheep #2
    Counting sheep #3
    Counting sheep #4
    Counting sheep #5
    Counting sheep #6
    Counting sheep #7
    Counting sheep #8
    Done counting sheep!
    ...script finished.
    

    Continue Output Example:
    (continues through loop, skipping any number containing a nine)

    Starting script...
    Counting sheep #1
    Counting sheep #2
    Counting sheep #3
    Counting sheep #4
    Counting sheep #5
    Counting sheep #6
    Counting sheep #7
    Counting sheep #8
    Counting sheep #10
    Counting sheep #11
    Counting sheep #12
    Counting sheep #13
    Counting sheep #14
    Counting sheep #15
    Counting sheep #16
    Counting sheep #17
    Counting sheep #18
    Counting sheep #20
    Counting sheep #21
    Counting sheep #22
    Counting sheep #23
    Counting sheep #24
    Counting sheep #25
    Counting sheep #26
    Counting sheep #27
    Counting sheep #28
    Counting sheep #30
    Counting sheep #31
    Counting sheep #32
    Counting sheep #33
    Counting sheep #34
    Counting sheep #35
    Counting sheep #36
    Counting sheep #37
    Counting sheep #38
    Counting sheep #40
    Counting sheep #41
    Counting sheep #42
    Counting sheep #43
    Counting sheep #44
    Counting sheep #45
    Counting sheep #46
    Counting sheep #47
    Counting sheep #48
    Counting sheep #50
    Counting sheep #51
    Counting sheep #52
    Counting sheep #53
    Counting sheep #54
    Counting sheep #55
    Counting sheep #56
    Counting sheep #57
    Counting sheep #58
    Counting sheep #60
    Counting sheep #61
    Counting sheep #62
    Counting sheep #63
    Counting sheep #64
    Counting sheep #65
    Counting sheep #66
    Counting sheep #67
    Counting sheep #68
    Counting sheep #70
    Counting sheep #71
    Counting sheep #72
    Counting sheep #73
    Counting sheep #74
    Counting sheep #75
    Counting sheep #76
    Counting sheep #77
    Counting sheep #78
    Counting sheep #80
    Counting sheep #81
    Counting sheep #82
    Counting sheep #83
    Counting sheep #84
    Counting sheep #85
    Counting sheep #86
    Counting sheep #87
    Counting sheep #88
    Done counting sheep!
    ...script finished.
    

    Return Output Example:
    (at the first sign of a nine, exit loop and function)

    Starting script...
    Counting sheep #1
    Counting sheep #2
    Counting sheep #3
    Counting sheep #4
    Counting sheep #5
    Counting sheep #6
    Counting sheep #7
    Counting sheep #8
    ...script finished.
    

    Exit Output Example:
    (at the first sign of a nine, kill the script)

    Starting script...
    Counting sheep #1
    Counting sheep #2
    Counting sheep #3
    Counting sheep #4
    Counting sheep #5
    Counting sheep #6
    Counting sheep #7
    Counting sheep #8
    


  • Thanks for the example. It makes so much sense now.


Log in to reply