Some 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 described here. Just to say, all logical operators treat false and nil as false, and everything else as true. 0 is also considered 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)

Results of the:

 

2. For the operator or, its first operand is assumed to be 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)

Results of the:


I have memorized the above two points before, but I found that after a period of time, I would forget or mix them up. Later, I summed up a method to memorize, and I will never mix them up again. This method is to associate memory according to the logical properties of and and or .


1). 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 there is a value of 0, it is 0 , and all are 1 . The formula value 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. We can return the first operand that is 0 ; assuming that the first operand is 1 , then the value of the entire expression is 0. 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.


2). 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 , so. When the first operand of or is 1 , we don't need to look at the following operands. At this time, the value of the expression is already 1 , and we can return the first operand whose value is 1 ; suppose The first operand is 0 . Then the value of the entire expression depends on the second operand. The second 0 expression has the value 0 , and the second 1 expression has the value 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:

3. When concatenating multiple operands, the return value of the expression is the first false value from left to right. 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:

 

4. When connecting multiple operands, the return value of the expression is the first non-false value from left to right. If all operand values ​​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:

 



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:

若 x>y,则 (x>y) = true,则 (x>y) and x = x,则 max = x or y = x;

若 x<y,则 (x>y) = false,则 (x>y) and x =  (x>y),则 max = (x>y) or y = y。

 

 

 

 

above.

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324884993&siteId=291194637