Lua-operators

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

Lua-オペレーター

演算子は、特定の数学的または論理的な操作を実行するようにインタープリターに指示する記号です。 Lua言語は組み込み演算子が豊富であり、次のタイプの演算子を提供します-

  • 算術演算子
  • 関係演算子
  • 論理演算子
  • その他の演算子

このチュートリアルでは、算術演算子、関係演算子、論理演算子、およびその他のさまざまな演算子を1つずつ説明します。

算術演算子

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

link:/lua/lua_arithmetic_operators [例を表示]

Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiply both operands A* B will give 200
/ Divide numerator by de-numerator B/A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0
^ Exponent Operator takes the exponents A^2 will give 100
- Unary - operator acts as negation -A will give -10

関係演算子

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

リンク:/lua/lua_relational_operators [例を表示]

Operator Description Example
== Checks if the value of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
~= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (A ~= B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A ⇐ B) is true.

論理演算子

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

link:/lua/lua_logical_operators [例を表示]

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言語でサポートされるその他の演算子には、*連結*および*長さ*が含まれます。

リンク:/lua/lua_miscellaneous_operator [例を表示]

Operator Description Example
.. Concatenates two strings. a..b where a is "Hello " and b is "World", will return "Hello World".
# An unary operator that return the length of the a string or a table. #"Hello" will return 5

Luaでの演算子の優先順位

演算子の優先順位は、式内の用語のグループ化を決定します。 これは、式の評価方法に影響します。 特定の演算子は、他の演算子よりも優先順位が高くなっています。たとえば、乗算演算子は加算演算子よりも優先順位が高い-

たとえば、x = 7 + 3 * 2;ここで、演算子*の優先順位は&plus;よりも高いため、xには20ではなく13が割り当てられます。したがって、最初に3 * 2で乗算され、7に加算されます。

ここでは、優先順位が最も高い演算子が表の上部に表示され、優先順位が最も低い演算子が下部に表示されます。 式内では、優先順位の高い演算子が最初に評価されます。

リンク:/lua/operators_precedence_in_Lua [例を表示]

Category Operator Associativity
Unary not # - Right to left
Concatenation .. Right to left
Multiplicative */% Left to right
Additive + - Left to right
Relational < > ⇐ >= == ~=  Left to right
Equality == ~= Left to right
Logical AND and Left to right
Logical OR or Left to right