+1 vote

what should the result be, for the code below ? thanks !
(edited: the result is True but it should be False, so I don't understand why is so)

extends Node
var a = false
var b = false
var c = true
var d = true
var e = false

func _ready():
 if a or d or c and e :
     print (true)
 else:
     print(false)
 pass
in Engine by (528 points)
edited by

2 Answers

+1 vote
Best answer

In binary operations, you can (basically) replace:
OR by + (addition)
AND by x (multiplication)
true by 1
false by 0
As in mathematics you always calculate multiplication before addition, you always check AND before OR

Which means:
a OR d OR c AND e could be written:
= a + d + c x e
= 0 + 1 + 1 x 0
= 0 + 1 + (1 x 0)
= 0 + 1 + 0
=1
= true

The correct result is True ;)

by (286 points)
selected by

As Omicron says: put parenthesis if you are unsure. That's the best way of not making mistakes ;)

+1 vote

Check this : http://docs.godotengine.org/en/stable/reference/gdscript.html#operators

Precedence of AND operator is higher than OR operator.

Anyway, without even checking docs, when coding, just use parenthesis if unsure.

by (338 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.