📜  Elm-运算符

📅  最后修改于: 2020-11-04 08:57:19             🧑  作者: Mango


 

运算符定义了将在数据上执行的一些函数。运算符作用的值称为操作数。考虑以下表达式

7 + 5 = 12

此处,值7、5和12是操作数,而+和=是运算符。

榆木的主要运算符可以分类为-

  • 算术
  • 关系型
  • 逻辑上

算术运算符

假设变量a和b中的值分别为7和2。

显示范例

Sr. No. Operator Description Example
1 +(Addition) returns the sum of the operands a+b is 9
2 -(Subtraction) returns the difference of the values a-b is 5
3 * (Multiplication) returns the product of the values a*b is 14
4 / (Float Division) performs division operation and returns a float quotient a / b is 3.5
5 //(Integer Division) performs division operation and returns a integer quotient a // b is 3
6 % (Modulus) performs division operation and returns the remainder a % b is 1

关系运算符

关系运算符测试或定义两个实体之间的关系类型。这些运算符用于比较两个或多个值。关系运算符返回布尔值,即true或false。

假设a的值为10, b的值为20。

显示范例

Sr. No. Operator Description Example
1 > Greater than (a > b) is False
2 < Lesser than (a < b) is True
3 >= Greater than or equal to (a >= b) is False
4 <= Lesser than or equal to (a <= b) is True
5 == Equality (a == b) is false
6 != Not equal (a != b) is True

可比类型

运算符(例如> =或<)可用于可比较类型。这些定义为数字,字符,字符串和列表,元组。运算符两侧的可比较类型必须相同。

Sr. No. Comparable Type Example
1 number 7>2 gives True
2 character ‘a’ ==’b’ gives False
3 string “hello” ==”hello” gives True
4 tuple (1,”One”)==(1,”One”) gives True
5 list [1,2]==[1,2] gives True

打开榆树REPL并尝试下面显示的示例-

C:\Users\admin>elm repl
---- elm-repl 0.18.0 -----------------------------------------------------------
:help for help, :exit to exit, more at 
--------------------------------------------------------------------------------
> 7>2
True : Bool
> 7.0>2
True : Bool
> 7.0<2.0
False : Bool
> 'a' > 'b'
False : Bool
> 'a' < 'b'
True : Bool
> "a" < "b"
True : Bool
> (1,2) > (2,3)
False : Bool
> ['1','3'] < ['2','1']
True : Bool
>

逻辑运算符

逻辑运算符用于组合两个或多个条件。逻辑运算符也返回布尔值。

显示范例

Sr. No. Operator Description Example
1 && The operator returns true only if all the expressions specified return true (10>5) && (20>5) returns True
2 || The operator returns true if at least one of the expressions specified return true (10 < 5) || (20 >5) returns True
3 not The operator returns the inverse of the expression’s result. For E.g.: !(>5) returns false. not (10 < 5) returns True
4 xor The operator returns true only if exactly one input returns true. The operator returns false if both the expressions return true. xor (10 > 5 ) (20 > 5) returns false