Confused with operators working with numbers

So basically if you know logical operators such as not, and, and or, you can tell where this is going.

print(3 and 1)

I am confused by this code line, it prints out 1 but it never prints out the number that is being compared, for example:

print(7 and 1)

Still prints 1!

print(11 and 1)

Still 1.
Even if I switch the and operator to or, it still prints out 1.
Can anyone explain how this works?

2 Likes

To concatenate two things together, you must do “” … “” – Also, you can’t just print out numbers. You must do tostring(1)

1 Like

and evaluates the first operand, if that value is falsy (false or nil), then it results in the first operand without evaluating the second operand, otherwise it evaluates and results in the second operand.

or, like and also evaluates the first operand, if that value is falsy, then it evaluates the returns second operand, otherwise it returns the first operand without evaluating the second operand.

https://www.lua.org/manual/5.1/manual.html#2.5.3


Factually incorrect. If you look at the source code, you’ll see that VM number (IEEE double) gets casted to strings when doing mostly anything with them in the Lua(u)-C boundary (concatenation in this case): Lua 5.1.5 source code - lvm.c

2 Likes

First of all, it’s .. and not ... and secondly you can actually print numbers you don’t have to do tostring() and finally this topic isn’t about printing it’s about logical operators.

You can’t use ... for string concatenation. It always should be .. (two dots) and I’m not upty, I just pointed out the false statements you said.

print(3 and 1)

is the same as

if 3 then
   print(1)
end
1 Like

This website will automatically convert .. to … so thats the cause of the confusion

Also, I like to show the short circuit behavior though a function and visuals, I think it explains it well

function AND(a,b)
    if a then
        return b
    else
        return a
    end
end

function OR(a,b)
    if a then
        return a
    else
        return b
    end
end

The reason this behavior can be done can be seen visually

Imagine the truth table of an AND statement:


This is similar to a multiplication table, but for an AND statement
You can see how if a is false, there is no way the result of the AND statement could be true, its always false
As a result of this, Lua will just return a, or false, to make it simpler

You can also see how if a is true, then the result of the AND statement is completely dependent on whatever b is
If b is false, it returns false
If be is true, it returns true
Therefore, Lua will just return b to make it simpler

A similar but opposite concept applies to OR statements


If a is false, the output is completely dependent on b, so Lua returns b
If a is true, the output is always true, so Lua simply returns a, which is true


This concept is very cool and useful and I think its probably one of my favorite Lua features
You can use it to do all kinds of fancy stuff, primarily a ternary statement, in a single line:

local result = condition and X or Y

--this is the same thing, but removes confusion about order of operations
local result = (condition and X) or Y

If condition is false or nil, it will return X
If condition is NOT false or nil, it will return Y

This works because if condition is false or nil, it will simply return itself
Once it returns itself, it will then do condition or Y, and it will return Y (or B) because condition is false or nil
If condition is true or exists, it will return X (the B term), which then gets compared with Y in X or Y
Because X is true or exists (Im assuming X and Y exist), it will always return X (or the A term)

You can use this to make sure stuff actually exists, you can use it to choose between different options based on a certain condition, and all kinds of stuff that would be super ugly in normal code

But yeah conditions epic

3 Likes

That isn’t my question. You’re being off topic.

So and evaluates first operand if falsy, and or evaluates second operand, so they are like the same but opposite right?

@a3nim
Ah! Because the and operator evaluates 3, which is a truey value, so it will evaluate the second operand, which is 1!

@PapaBreadd

Thank you the 3 of your helpful words.

He was curious about the weird behavior of boolean statements and how they work when given stuff that isn’t a boolean, which is where the confusion started I think
He wasn’t trying to combine the two numbers to print them, he was asking about why it printed what it does
There’s nothing wrong with putting an and in a print statement, it just prints the result of whatever you put into it