Take the 2-minute tour ×

Is there a way to execute a line of code only if multiple statements are false?

I have an inventory:

inventory = {
    {"Images/Category 1/pistol1.png", false},
    {"Images/Category 1/machinePistol1.png", false},
    {"Images/Category 2/shotgun1.png", false},
    {"Images/Category 2/assaultRifle1.png", false},
    {"Images/Category 3/sniperRifle1.png", false},
    {"Images/Category 3/rocketLauncher1.png", false}

And I'd like to write a function that executes a line of code if all those statements are false, but obviously executes something else if even one of them is true.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

You can use a variable, and assume it's true

local IsEverythingTrue = true

-- the **for** statement is a loop. It will allow us to operate
-- the same way on any number elements in inventory.
-- _,v stands for variables that we are going to read from inventory table
-- we can't do it directly, however; **ipairs** function will prepare it so
-- _ will hold numerical index of each element (1, 2, 3 and so on);
-- we don't use it, though, so I put in a placeholder name
-- v will hold every value, so your two-element table

for _,v in ipairs(inventory) do

    -- if any value of v[2] is false, break
    -- v[2] is equal to inventory[_][2]
    -- if not v[2] can be written as 
    -- *if value of v[2] isn't true*

    if not v[2] then
        -- in this case, after first non-true element has been found
        -- we know that not every one is true, or there is at least one false
        IsEverythingTrue = false
        break -- we don't have to check anything else

Then use that variable in expression

if IsEverythingTrue then
    -- do something
    -- do something else

If you want it to execute with multiple falses, just count them. Add local falseCount = 0 at the beginning, and change break for falseCount = falseCount + 1.

share|improve this answer
That seems awfully complicated... I actually managed to solve it just now by using if (inventory[1][2] == false and inventory[2][2] == false and, etc, etc... Not the prettiest code, but it seems to work –  Cleverbird Jan 17 '13 at 11:25
@Cleverbird What if you'll want to add something to the inventory? You will need to add another clause to your if statement. My approach is just a for loop, which will work regardless of number of elements in inventory. Besides, you can put this code into function, just like you wanted to. –  Bartek Banachewicz Jan 17 '13 at 11:43
@Cleverbird it seems you don't understand some really basic concepts of programming such as loops thus I recommend reading some tutorials or documentation first. –  Miszy Jan 17 '13 at 11:48
The thing is, I need to be able to understand the code myself, and I really cant tell what that snippet of code really does. I'd rather not blindly copy paste code, without knowing what it does. –  Cleverbird Jan 17 '13 at 12:04
@Cleverbird that's not a problem. You should've asked for clarification at once. –  Bartek Banachewicz Jan 17 '13 at 12:13

Your Answer


