Page 1 of 1

2 if statements

Posted: Thu Sep 17, 2020 6:54 am
by liamhvet
hello i need i want to do something like this

if AAAAAAAA and AAAAAAAB then
love.graphics.print("blahblah")
end

Re: 2 if statements

Posted: Thu Sep 17, 2020 1:04 pm
by RockEt__2580__
Sorry am a noob but I do it like this
don't know if its the right way but it works

if AAAAAAAA then
if AAAAAAAB then
love.graphics.print("bladi bladi")
end
end


Hope it helps

Re: 2 if statements

Posted: Thu Sep 17, 2020 3:30 pm
by Jeeper
I think you asked the same question on the Love2d subreddit, but just in case I will answer it here as well.
While you could nest "if"-statements like "RockEt__2580__" suggested I would recommend using "and". It's a cleaner solution and it's meant for exactly this situation.

Code: Select all

if a == 10 and b == 20 then
	print("hello world")
end
This will trigger if a is 10 and b is 20. It will NOT trigger if only a is 10 or if only b is 20. If you want it to tigger when either of them is true, then you need to use "or".

Re: 2 if statements

Posted: Thu Sep 17, 2020 6:05 pm
by zorg
One more thing i can think of about how one could logic out stuff wrong:

Code: Select all

if a == 10 and 20 then
-- blah
end
if a == 10 or 20 then
-- blah
end
These will work too, but not how and why you might think.
"10 and 20" and "10 or 20" might be logical in english when you combine it with the question "is the variable a equal to...", but lua code doesn't work that way. you need to duplicate the variable and the check for all logical clauses. in my example, 20 will always evaluate to true, since it's truthy, just like anything else that's not false or nil.