Can I "break out" of an if{} block?

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Jun 9, 2006.

  1. Guest

    At any point in a loop, I can "break out" of it with a 'last', or I can
    "break out" of a subroutine with a 'return'.

    Is there a similar way to easily "break out" of an if{} block (without
    a goto)?

    Thanks!

    --
    David Filmer (http://DavidFilmer.com)
    , Jun 9, 2006
    #1
    1. Advertising

  2. wrote:
    > At any point in a loop, I can "break out" of it with a 'last', or I can
    > "break out" of a subroutine with a 'return'.
    >
    > Is there a similar way to easily "break out" of an if{} block (without
    > a goto)?


    Sure, just put a loop inside the if block:

    if ( $expression ) { {
    last if $oops;
    } }


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Jun 9, 2006
    #2
    1. Advertising

  3. <> wrote:
    > At any point in a loop, I can "break out" of it with a 'last', or I can
    > "break out" of a subroutine with a 'return'.
    >
    > Is there a similar way to easily "break out" of an if{} block (without
    > a goto)?



    Yes, by nesting another if:

    if ( $something ) {
    # do this always
    if ( ! $want_to_last ) { # unless ( $want_to_last ) is "nicer"
    # do this when not "lasting"
    }
    }


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 9, 2006
    #3
  4. Ben Morrow Guest

    Quoth :
    > At any point in a loop, I can "break out" of it with a 'last', or I can
    > "break out" of a subroutine with a 'return'.
    >
    > Is there a similar way to easily "break out" of an if{} block (without
    > a goto)?


    Any block (except for the pseudoblocks which are part of if{}, else{},
    do{} &c.) is treated as a loop which loops once. So just add another set
    of braces and use 'last':

    if (1) {{
    #...
    last if #...
    #...
    }}

    Ben

    --
    All persons, living or dead, are entirely coincidental.
    Kurt Vonnegut
    Ben Morrow, Jun 9, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BartlebyScrivener

    keyboard command to break out of infinite loop?

    BartlebyScrivener, Apr 16, 2006, in forum: Python
    Replies:
    5
    Views:
    859
    BartlebyScrivener
    Apr 16, 2006
  2. Noodle

    Double break out of a loop

    Noodle, Aug 8, 2006, in forum: Java
    Replies:
    3
    Views:
    25,341
    Noodle
    Aug 8, 2006
  3. morrell
    Replies:
    1
    Views:
    946
    roy axenov
    Oct 10, 2006
  4. Replies:
    0
    Views:
    681
  5. bharathbhushan.kn

    how to break out of for loop

    bharathbhushan.kn, Mar 11, 2009, in forum: VHDL
    Replies:
    1
    Views:
    10,276
    jeppe
    Mar 11, 2009
Loading...

Share This Page