# Wacky conditional statement

I’m trying to understand somehting in lua that was recently brought up to me.
People tell me to use the 'and or" method instead of an if statement. I really don’t know why this works in Lua, but here’s what I mean:

``````part.Position = Vector3.new(0,0,0) and print("At origin") or print("Not at origin")
``````

and apparentely this is the same thing as

``````if part.Position == Vector3.new(0,0,0) then
print("At origin")
else
print("Not at origin")
end
``````

I’m just trying to figure out why this doesn’t error when ran.

1 Like

I just recently learned it so I’ll give a simple explaination:

`Vector3.new(0,0,0)` is the conditional statement, meaning that `and`/`or` will be used depending on if that first statement is true or not. If the conditional statement is true, what is after the `and` will be used. If the conditional statement is false, what is after the `or` will be used.

Keep in mind this is just a simplified (and probably more optimized) way to write an if statement.

it is the same thing, saying and is basicly saying your setting it to Vector2.new(0,0,0) then print(“At origin”) saying or is basicly like saying else

1 Like

I already kinda know how it works, I’m just wondering why it works.

lua returns the last variable checked in a boolean operation. So if you were to say something like:

``````a = number or 1
``````

If number exists, it will return number because if the first argument of an or is true, the statement returns true so it doesn’t bother to look at the next thing because the whole statement will be true if the first one is. However, if it’s false or nil it will go to the next argument and return that instead if it’s truthy (not false or nil). In this case it would return 1.

ands are a bit different

``````a = number and 1
``````

In the case of an and, it has to check everything. Well everything until it hits one that is false. Once it hits a false value, the and statement will be false regardless of what’s past it so it will stop checking. So in this case if number is truthy it will return 1 since it had to check to see if the 1 is truthy, but if it was false, it would have immediatly stopped and returned false.