Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

exiting a if loop

by toadi (Chaplain)
on Jun 06, 2000 at 16:56 UTC ( [id://16593]=perlquestion: print w/replies, xml ) Need Help??

toadi has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
got a if loop to test some things:

if(HOLIDAY){
$check = TRUE;
exit;
}
else if ( $time <= 1800){
$check = TRUE;
exit;
}

with the exit I mean wat is the thingie to exit the if-loop and continue the rest of the program.
So I want to check statement 1 true, ok continue the program and don't check further.

Is there something like the exit thingie in c?

Replies are listed 'Best First'.
Re: exiting a if loop
by fundflow (Chaplain) on Jun 06, 2000 at 19:13 UTC
    Why use TRUE/FALSE instead of the standard 1/0?

    This could be:
    $check = ((HOLIDAY) || ($time <= 1800));
Re: exiting a if loop
by nuance (Hermit) on Jun 06, 2000 at 17:55 UTC
    The if statement already works in the manner that you have asked for. A cut down version of your code below shows how this works:

    if(HOLIDAY){ $check = TRUE; } elsif ( $time <= 1800){ $check = TRUE; }
    This does what you asked for and as you can see you don't need the exit statements. Perl evaluates the "if/else if" statements in turn until one of the conditions is true. It then executes the block immediately following that statement and then jumps to the end of the entire conditional. Thus it only does the "first" conditional block that it can. Several other languages don't work that way (c's switch statement for instance needs breaks) and that may have confused you, but Perl already does what you want by default.

    You realise of course that you example you gave can be written more succintly as:

    $check = TRUE if ((HOLIDAY) or ($time <= 1800))
    If this hasn't covered the whole subject to your satisfaction, if you post more of what you're trying to do then we can elaborate.

    update As le and flyfishing have pointed out I got the else if thing wrong. I've changed the node so that it reads properly.

    Nuance

    Baldrick, you wouldn't see a subtle plan if it painted itself purple and danced naked on top of a harpsichord, singing "Subtle plans are here again!"

      I guess that should be
      if(HOLIDAY){ $check = TRUE; } elsif ( $time <= 1800){ $check = TRUE; }
        Which is basically what I wrote, what with white space being irrelevant and all. Hey this isn't Python :-)

        Lets face it:

        if(HOLIDAY){$check = TRUE;} elsif ( $time <= 1800){$check = TRUE;}
        Would also work, even if it is slightly less readable

        Nuance

        Baldrick, you wouldn't see a subtle plan if it painted itself purple and danced naked on top of a harpsichord, singing "Subtle plans are here again!"

      > You realise of course that you example you gave
      > can be written more succintly as:

      > $check = "TRUE" if ((HOLIDAY) or ($time <= 1800))

      For more succinctness, leave out the parens:

      $check = TRUE if $HOLIDAY or $time<=1800;
Re: exiting a if loop
by buzzcutbuddha (Chaplain) on Jun 06, 2000 at 18:57 UTC
    hows about:
    $check = TRUE if ((HOLIDAY) || ($time <= 1800));
    or
    ((HOLIDAY) || ($time <= 1800)) ? $check = TRUE : $check = FALSE;
    ;)

      Ah! A foul use of the beloved ternary test! 1 Don't put the lvalue in their twice. Do this instead: 2

      $check = $HOLIDAY || $time<=1800 ? "TRUE" : "FALSE";

      [1] TIMTOWTDI, of course, I'm not saying it's "wrong", just inelegant :)

      [2] Note that the use of "or" instead of "||" would be wrong in this case, as ternary is higher than "or", but lower than "||", in precedence.

        I concede that I did not make the best use of the ternary operator. oh well. now I know better.
        thanks for the tip Turnstep.
Re: exiting a if loop
by lhoward (Vicar) on Jun 06, 2000 at 17:00 UTC
    perldoc perlfunc

           last    The `last' command is like the `break' statement
                   in C (as used in loops); it immediately exits the
                   loop in question.  If the LABEL is omitted, the
                   command refers to the innermost enclosing loop.
                   The `continue' block, if any, is not executed:
    
                       LINE: while (<STDIN>) {
                           last LINE if /^$/;      # exit when done with header
                           #...
                       }
    
                   `last' cannot be used to exit a block which
                   returns a value such as `eval {}', `sub {}' or `do
                   {}', and should not be used to exit a grep() or
                   map() operation.
    
                   Note that a block by itself is semantically
                   identical to a loop that executes once.  Thus
                   `last' can be used to effect an early exit out of
                   such a block.
    
    
RE: exiting a if loop
by nuance (Hermit) on Jun 06, 2000 at 23:27 UTC
    Several of the last few posts assign both true and false to $check. The original statement in the question does not.

    Turnsteps post is the last one that got it right, but if you want to use the ternary operator then this does it:

    $check = HISTORY || $time <= 1800 ? TRUE : $check;

    Nuance

RE: exiting a if loop
by le (Friar) on Jun 06, 2000 at 17:17 UTC
    if is not a looping function, so I don't think you can break out.
      Sure you can. Check the last para in the doc:
      Note that a block by itself is semantically identical to a loop that executes once. Thus `last' can be used to effect an early exit out of such a block.
        An "if block" is not considered a looping construct on it's own, it's an exception, so the loop exit statements don't work. What you can do is make the "if block" contain just a "bare block". Since a bare block is considered a loop then it works, so:
        if (true) { yada; yada; next if $foo; yada; yada; }
        Doesn't work,
        if (true) { { yada; yada; next if $foo; yada; yada; } # next comes here }
        Does.

        Nuance

        Baldrick, you wouldn't see a subtle plan if it painted itself purple and danced naked on top of a harpsichord, singing "Subtle plans are here again!"

      There is always the dreaded goto function :)

      (I feel dirty even mentioning it)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://16593]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-26 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found