Lua-logical-operators

提供:Dev Guides
移動先:案内検索

Lua-論理演算子

次の表は、Lua言語でサポートされているすべての論理演算子を示しています。 変数 A がtrueを保持し、変数 B がfalseを保持すると仮定します-

Operator Description Example
and Called Logical AND operator. If both the operands are non zero then condition becomes true. (A and B) is false.
or Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. (A or B) is true.
not Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A and B) is 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