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

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.

主站蜘蛛池模板: 乌兰察布市| 故城县| 安远县| 固镇县| 九寨沟县| 井陉县| 濉溪县| 高陵县| 深州市| 漠河县| 大悟县| 江津市| 洪江市| 合山市| 武宣县| 临沧市| 鄯善县| 明星| 峨山| 溧水县| 绥滨县| 海宁市| 桦南县| 内黄县| 大渡口区| 若尔盖县| 内江市| 荆州市| 泰兴市| 克什克腾旗| 杭锦后旗| 安陆市| 永安市| 股票| 九龙坡区| 景德镇市| 房产| 桐梓县| 南靖县| 库车县| 项城市|