This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+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)
in Engine by (530 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
= true

The correct result is True ;)

by (288 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 :

Precedence of AND operator is higher than OR operator.

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

by (340 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 Frequently asked questions and 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 [email protected] with your username.