Some normal and special usages of and and or in Lua

Logical operators in Lua: and (and), or (or) and not (not) , which are consistent with the functions of logical operators in other languages, and will not be repeated here. Just to say, all logical operators treat false and nil as false, anything else as true, and 0 as true.

What I want to talk about here are some special uses of and and or.

When concatenating 2 operands:

1. For the operator and, if its first operand is false, it returns the first operand; otherwise, it returns the second operand.

so:

a = b and c

Equivalent to:

if not b then
  a = b
else 
  a = c
end

Take a chestnut:

print(2 and 3)
print(0 and 3)
print(nil and 7)
print(false and 7)

Execution result:
insert image description here
2. For operator or, assume that its first operand is true. Returns the first operand, otherwise returns the second operand.

so:

a = b or c

Equivalent to:

if b then
  a=b
else
  a=c
end

Take a chestnut:

print(4 or 5)
print(0 or 5)
print(nil or 8)
print(false or 8)

Execution result:
insert image description here
I had memorized the above two points before, but I found that I would forget or confuse it after a period of time. Later, I summed up a method to memorize, and I will never confuse it again. This method is to associate memory according to the logical properties of and and or.

Let’s talk about and first: we know that and is an AND, and is 0 and 0 = 0, 0 and 1 = 0, 1 and 1 = 1, that is to say, if one value is 0, it is 0, and all expressions are 1. is 1. So, when the first operand of and is 0. We don't need to look at the following operands. At this time, the value of the expression is already 0, and we can return the first operand that is 0; assuming that the first operand is 1, then the entire expression The value depends on the second operand, and the second is 0. The value of the expression is 0. The second is 1 The expression value is 1, so since the second operand is whatever the expression value is. Then we just return the second operand.

Besides or, it is also a truth: or is to take or. Or just 0 or 0 = 0, 0 or 1 = 1, 1 or 1 = 1. That is to say, if there is a value of 1, it is 1, and the value of all 0 expressions is 0. Therefore, when the first operand of or is 1, we do not need to look at the following operands. At this time, the value of the expression is already 1, so we can return the first operand whose value is 1. ; Assume the first operand is 0. Then the value of the entire expression depends on the second operand. The second is a 0 expression with a value of 0, and the second is a 1 expression with a value of 1. So since the second operand is what the expression value is, we can just return the second operand.

All right. According to my method above, the result can also be deduced when there are multiple operands:

When and concatenates multiple operands, the return value of the expression is the first value from left to right that is false. If all operand values ​​are not false. The return value of the expression is the last operand.

Take a chestnut:

print(2 and 3 and 4 and nil and false and 7 and 8)  
print(2 and 3 and 4 and 5 and 6 and 7 and 8) 

Results of the:

insert image description here
When or concatenates multiple operands, the return value of the expression is the first non-false value from left to right. If all operands are false, the return value of the expression is the last operand.

Take a chestnut:

print(nil or false or 6 or 7 or 8)   
print(nil or false ) 
print(false or nil ) 

Results of the:
insert image description here

In addition, "a and b or c" is often used in Lua, which is similar to the expression a ? b : c in C language, such as selecting the larger of x and y. The following statement can be used:

max = (x>y) and x or y

Proof such as the following:

If x>y, then (x>y) = true, then (x>y) and x = x, then max = x or y = x.

So x<y, and (x>y) = false, and (x>y) and x = (x>y), and max = (x>y) or y = y.

Normal usage:

true and false result in false

true or false results in true

Special usage:

true and number the result is a number

true or number The result is true

false and numbers get the result false

false or number The result is a number

Guess you like

Origin blog.csdn.net/qq_44918090/article/details/126511481