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

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.

主站蜘蛛池模板: 谷城县| 洛南县| 广河县| 贵州省| 浏阳市| 正镶白旗| 稷山县| 鹤庆县| 河北省| 枣阳市| 县级市| 新和县| 卓资县| 耒阳市| 宁蒗| 邵武市| 视频| 蒙阴县| 陆河县| 青川县| 武定县| 信丰县| 钟祥市| 广元市| 叙永县| 炎陵县| 甘南县| 长丰县| 阿鲁科尔沁旗| 常德市| 湖南省| 都昌县| 梅河口市| 盖州市| 三都| 巴塘县| 林周县| 电白县| 疏勒县| 宣城市| 右玉县|