Does 2nd conditions skip if there's [or] and 1st conditions is true?

Hi, I’m developer. just a quick question, does 2nd conditions skip if there’s [or] and 1st conditions is true?
Just like:

if true or false then
     print("foo bar")
end

This will print foo bar of course because 1st conditions is true, but does they skip [false] conditions? (2nd)
I’m sorry that this is basic questsion and my curious thinkings and not following to template.
thank you!

Well the answer to this question is relatively straight forward.

When using the “or” statement you are simply checking for one of the parameters to be true.

So when you put “true or false” the first parameter is true therefore running that block of code however, if you put false or true the block of code will still run due to the second parameter being true. Hope that helps! :smile:

1 Like

Oh, then code running should be like this?
image

local function test(str, bool)
	print(str)
	return bool
end

-- prints: A, inside
if test("A", true) or test("B", true) then
	print("inside")
end
2 Likes

Yeah exactly, so again it would just always fire depending on if one of the conditions is true.

1 Like

I tried to make quick codes to proof that’s true, and It’s now real!

local function yield()
	print("2nd try")
	wait(5)
	return false
end

if true or yield() then
	print("foo bar")
end

returned “foo bar”. Thank you!

Yes, it skips the 2nd condtion if the first condition is a truthy (Literally every value except false and nil in Lua) value.

print(true or (1 + nil))

You can see this for JavaScript too

console.log(true || 1n+2)
console.log(1n+2) // errors

Though this isn’t the case for C# (and probably C and C++)

using System;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine(true || (true + 1)); // 
Compilation error (line 7, col 30): Operator '+' cannot be applied to operands of type 'bool' and 'int'
	}
}

This also apply to falsy (false and nil in Lua, 0, '' false, None, and many more in Python, undefined, +0, NaN, -0, 0n, "", null in Javascript) values and the and statement

print(false and (1 + nil)) --> false
print(nil and (1 + nil)) --> nil
print(true and (1 + nil)) --> Errors
console.log(undefined && (1n+1)) // undefined
console.log(true && (1n+1)) // errors
1 Like

When a comparison returns true the code just runs on regardless of any other comparison made after the or.

1 Like

This is called lazy evaluation btw. Programming in Lua : 3.3

1 Like