2 if statements

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
liamhvet
Prole
Posts: 2
Joined: Mon Sep 14, 2020 3:23 pm

2 if statements

Post by liamhvet »

hello i need i want to do something like this

if AAAAAAAA and AAAAAAAB then
love.graphics.print("blahblah")
end
RockEt__2580__
Prole
Posts: 12
Joined: Mon Sep 14, 2020 8:36 am

Re: 2 if statements

Post 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
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: 2 if statements

Post 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".
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: 2 if statements

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Nikki and 2 guests