官术网_书友最值得收藏!

Operators

Operators such as addition +, string concatenation .., and even the assignment operator = have been used throughout this book. Let's take some time to cover in detail what operators are and how they work. Operators fall into one of the following categories:

  • Arithmetic operators do math.
  • Relational operators always return a Boolean value: true or false. Relational operators are used to compare the relationship between two things, for example, by checking whether one number is smaller than another number.
  • Logical operators express complex relations such as and/or. For example, logical operations can be used to check whether a number is less than seven AND greater than two.
  • Misc operators: All other operators, such as assignment, fall into this category.


Operators can be unary or binary. A unary operation works on only one operand. For example, the minus sign (-) is the unary negation operator. It returns the negative value of a number:

x = -7 -- negation operator applied to the constant 7
y = -x -- negation operator applied to the x variable
-- x: -7, y: 7

A binary operator on the other hand operates on two operands. The binary subtraction operator also uses the minus sign (-), but it is a completely different operator from its unary counterpart. An example of the binary subtraction operator would be:

x = 7 - 3 -- Operand 1 is the constant 7, Operand 2 is the constant 3
y = x - 1 -- Operand 1 is the variable x, Operand 2 is the constant 1
z = x - y -- Operand 1 is the variable x, Operand 2 is the variable y

Most operators will be binary, that is, they will work on two operands.

主站蜘蛛池模板: 安阳市| 甘谷县| 灵寿县| 柘荣县| 四子王旗| 阿图什市| 铜川市| 南阳市| 安泽县| 北碚区| 友谊县| 通州市| 山阳县| 宜兰市| 共和县| 泸定县| 综艺| 沙河市| 丁青县| 顺义区| 芷江| 抚顺县| 安多县| 北票市| 荆州市| 新晃| 孝感市| 锡林郭勒盟| 牟定县| 高台县| 商都县| 濮阳市| 南溪县| 长寿区| 福州市| 南城县| 乾安县| 兰坪| 尖扎县| 木兰县| 盐边县|