Trouble Understanding ]Conditionals

Hi guys. I’m a beginner scripter and I have this question about conditionals (if statements)

Here are 2 scripts:
1.

					if Value.Name == "GoldCoast" and Value.Value == false or Value.Name == "Intermarium" and Value.Value == false then
						E = E + 1
					end
					if Value.Name == "GoldCoast" or Value.Name == "Intermarium" and Value.Value == false then
						E = E + 1
					end

For the second script, would the

and Value.Value == false

Apply to both "Value.Name == “Something” "

or just the condition next to it.
Does that mean the “ÖR” separates an if statement conditionals?

I am sorry if this is really beginner question. I am just really curious as I tested both 1 and 2 in scripts and both gave different results, and I can’t seem to understand why

All responses are all appreciated. Sorry if this was a stupid question.

Lua evaluates if statements left to right known as short-cut statements as lua documentation states,

Both and and or use short-cut evaluation, that is, they evaluate their second operand only when necessary.

3 Likes

I still dont really understand. What do you mean by “they evaluate their second operand only when necessary.”

What it means is that lua does not evaluate operands that do not affect the truth value of the condition.

Lua and underlaying C evaluation logic uses different gates found on your motherboard, including AND-, OR-, NAND-, NOR-gate etc.

Lua starts evaluating on the left, as dthecoolest said. In more complex expressions, if the first part of the condition can determine the truth value of the whole if-statement, then the rest may be ignored.

Here is a list of all possible variable combinations and truth values in lua for your two if-statements:

Let’s take combination [1 0 1] as an example (A = true, B = false, C = true):

A == true → true
C == false → false
B == true → false

A == true and C == false or B == true and C == false:

if true and false or false and false ==> false

A == true or B == true and C == false:

if true or false and false then ==> true

In the second if-statement, if A turns out to be truthy, lua will conclude that the whole statement is true and proceed.

And so forth.

3 Likes

idk if this is what you want but here are some script examples;

local a = true
local b = true
--this checks if either value is true, going to the first one and seeing if it
--is true, then if it isn't it will go to the next to see if that one is true to,
--but if it was true it will stop because it already got what it needed.
if a or b then -- we dont need to put == to for true false variables.
   print("a or b is true")
end
--this will check if it meets the first condition, and the second condition, first
-- checking if a is true and then if it is it checks if b is true but if a wasn't 
--true then it would stop because it would need both of them to be true.
if a and b then
   print("a and b are true")
end

--one thing you can do with these is going something like
if a and not b or not a and b then
   print("a is true but b isn't or b is true and a isn't")
end
--doing that makes it so you can check if different groups of conditions
--are true like if these two conditions both met, or if these two conditions are
--both met. The OR acts as a separator that can make it so that you can have
--multiple groups of conditions that will let a signal through if one of those
--groups of conditions is met. Of course you can still just use or for single
--conditions. Conditionals are almost always used in any script to help control what happens.

Don’t know if this is a good explanation or not.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.