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

U

usenet

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!
 
J

John W. Krahn

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
 
T

Tad McClellan

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"
}
}
 
B

Ben Morrow

Quoth (e-mail address removed):
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top