下表顯示了Lua語言支持的所有邏輯運算符。 假設變數A=true
,變數B=false
,則 -
運算符 | 描述 | 示例 |
---|---|---|
and |
邏輯與運算符。如果兩個運算元都不為零,則條件成立。 | (A and B) 結果為false |
or |
邏輯或運算符。 如果兩個運算元中的任何一個不為零,則條件變為真。 | (A or B) 結果為true |
not |
邏輯非運算符。用於反轉其運算元的邏輯狀態。 如果條件為真,則邏輯非運算符將為false 。 |
!(A and B) 結果為true |
示例
嘗試以下示例來瞭解Lua編程語言中可用的所有邏輯運算符 -
a = 5
b = 20
if ( a and b )
then
print("Line 1 - Condition is true" )
end
if ( a or b )
then
print("Line 2 - Condition is true" )
end
--lets change the value ofa and b
a = 0
b = 10
if ( a and b )
then
print("Line 3 - Condition is true" )
else
print("Line 3 - Condition is not true" )
end
if ( not( a and b) )
then
print("Line 4 - Condition is true" )
else
print("Line 3 - Condition is not true" )
end
當構建並執行上述程式時,它會產生以下結果 -
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is true
Line 3 - Condition is not true