nab
nab

Reputation: 4861

Lua: nested if statements

Lua is a light and powerful language, but sometimes it feels like lacking some very convenient features we get used to in other languages. My question is about nested if conditions. In Perl, Python, C++ I generally tend to avoid nested constructions and write plain code whenever possible like:

# Perl:
for (my $i = 0; $i < 10; ++$i) {
    next unless some_condition_1();
    next unless some_condition_2();
    next unless some_condition_3();
    ....
    the_core_logic_goes_here();        
}

Lua is missing that next or continue statement, so the same code will look like:

-- Lua:
for i = 1, 5 do
    if some_condition_1() then
        if some_condition_2() then
            if some_condition_3() then
                the_core_logic_goes_here()
            end
        end
    end
end

So I'm wondering if there's standard approach to avoid nested if blocks in Lua?

Upvotes: 4

Views: 6110

Answers (5)

Hedron
Hedron

Reputation: 86

Solution 1.

You could just add all you conditions to the if statement and use a else statement, which you should do. So something like this:

if cond_1() and cond_2() and cond_n() then
  the_core_logic_goes_here()
else
  -- do something else here
end

Solution 2.

You could use something that similar but looks more like in the other languages you're more comfortable in which is just to do if cond_n() then else return end which just returns nil if cond_n() is not met. All together it should look something like this:

for idx=1, 5 do
  if cond_1() then else return end
  if cond_2() then else return end
  if cond_3() then else return end
  if cond_4() then else return end
  the_core_logic_goes_here()
end

However, I really think you should use the former, its a much better solution and I'm pretty sure lua Solution 1. will compile into faster bytecode.

Upvotes: 0

prapin
prapin

Reputation: 6898

On Lua 5.2, you can use the goto statement (with care please)!

One of the typical usage for that keyword is to replace the missing continue or next statement.

for i = 1, 5 do
  if not some_condition_1() then goto continue end
  if not some_condition_2() then goto continue end
  if not some_condition_3() then goto continue end
  the_core_logic_goes_here()
::continue::
end

Upvotes: 6

Nathan
Nathan

Reputation: 430

for v in pairs{condition1,condition2,condition3} do
    if  v() then
        the_core_logic_goes_here()
    end
end

Might be of your liking?

"Lua is missing that next or continue statement" Lua as a next statement and a very similar function "ipairs".

Upvotes: 0

Mud
Mud

Reputation: 29021

sometimes it feels like lacking some very convenient features we get used to in other languages

The trade-off is economy of concepts, which results in implementation simplicity, which in turn results in Lua's famous speed and small size.

As for your code, this is not the broadest solution (see the other respondents for two means of implementing continue), but for your specific code I'd just write:

for i = 1, 5 do
    if  some_condition_1() 
    and some_condition_2() 
    and some_condition_3() then
        the_core_logic_goes_here()
    end
end

Upvotes: 4

Lily Ballard
Lily Ballard

Reputation: 185861

I don't know if this is particularly idiomatic, but you could use a single nested loop along with break to simulate continue

for i = 1, 5 do
    repeat
        if some_condition_1() then break end
        if some_condition_2() then break end
        if some_condition_3() then break end
        the_core_logic_goes_here()
    until true
end

Upvotes: 5

Related Questions