How do you continue in a for loop?

L

Lars Haugseth

* Lawrence Statton said:
Yeah -- I like your bridesmaid code better, too ... I'm still trying
to think of a good example before breakfast. I need caffeine and
sugar and lipids to write ...

I prefer the 'next' method when there are several conditions that could
cause a list item to be skipped (without generating an exception of any
kind):

for my $item (@list) {
next if ($item->{'Foo'} < 0);
next if (!defined $item->{'Bar'});
next if ($item->{'Baz'} eq 'IGNORE');

<do stuff with $item>
}

I like the above better than the following:

for my $item (@list) {
if ($item->{'Foo'} >= 0 &&
defined $item->{'Bar'} &&
$item->{'Baz'} ne 'IGNORE')
{
<do stuff with $item>
}
}

...especially when the conditionals are complex and/or hard to read,
and there are many of them.

There's also the added bonus of getting one less level of indentation.

In addition, it makes it pretty clear up front that these are special
cases and not the norm.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top