Elixir-example-comparision
提供:Dev Guides
Elixir-比較演算子
Elixirの比較演算子は、他のほとんどの言語で提供されている演算子とほとんど共通しています。 次の表は、Elixirの比較演算子をまとめたものです。 変数 A が10を保持し、変数 B が20を保持すると仮定します-
Operator | Description | Example |
---|---|---|
== | Checks if value on left is equal to value on right(Type casts values if they are not the same type). | A == B will give false |
!= | Checks if value on left is not equal to value on right. | A != B will give true |
=== | Checks if type of value on left equals type of value on right, if yes then check the same for value. | A === B will give false |
!== | Same as above but checks for inequality instead of equality. | A !== B will give true |
> | Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. | A > B will give false |
< | Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. | A < B will give true |
>= | Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. | A >= B will give false |
⇐ | Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true. | A ⇐ B will give true |
例
次のコードを試して、Elixirのすべての算術演算子を理解してください。
a = 10
b = 20
IO.puts("a == b " <> to_string(a == b))
IO.puts("a != b " <> to_string(a != b))
IO.puts("a === b " <> to_string(a === b))
IO.puts("a !== b" <> to_string(a !== b))
IO.puts("a > b " <> to_string(a > b))
IO.puts("a < b " <> to_string(a < b))
IO.puts("a >= b " <> to_string(a >= b))
IO.puts("a <= b " <> to_string(a <= b))
上記のプログラムを実行すると、次の結果が生成されます-
a == b false
a != b true
a === b false
a !== b true
a > b false
a < b true
a >= b false
a <= b true