跳转到内容

Dragon 语言入门 / 课程 / 运算符

来自维基教科书,自由的教科书

运算符

[编辑 | 编辑源代码]

本章将介绍 Dragon 编程语言提供的运算符。

算术运算符

[编辑 | 编辑源代码]

下表展示了 Dragon 语言提供的算术运算符。假设变量 x = 50,变量 y = 10,则

+------------+---------------+----------+---------+
| Operator   | Description   | Example  | Result  |
+============+===============+==========+=========+
| +          |  Add          |  x+y     |  60     |
+------------+---------------+----------+---------+
| -          |	Subtract     |	x-y     |  40     |
+------------+---------------+----------+---------+
| *          |  Multiply     |	x*y     |  500    |
+------------+---------------+----------+---------+
| /          |  Divide	     |	x/y     |  5      |
+------------+---------------+----------+---------+
| %          |  Modulus	     |	x%y     |  0      | 
+------------+---------------+----------+---------+
| ++         |  Increment    |	x++     |  51     |
+------------+---------------+----------+---------+
| --         |  Decrement    |	x--     |  49     |
+------------+---------------+----------+---------+

关系运算符

[编辑 | 编辑源代码]

下表展示了 Dragon 语言提供的关系运算符。假设变量 x = 50,变量 y = 10,则

+------------+---------------------+-------------+---------+
| Operator   | Description         | Example     | Result  |
+============+=====================+=============+=========+
| ==         |  Equal	           |    x == y   |  False  |
+------------+---------------------+-------------+---------+
| !=         |	Not Equal          |	x != y   |  True   |
+------------+---------------------+-------------+---------+
| >          |  Greater than       |	x > y    |  True   |
+------------+---------------------+-------------+---------+
| <          |  Less than          |	x < y    |  False  |
+------------+---------------------+-------------+---------+
| >=         |  Greater or Equal   |	x >= y   |  True   | 
+------------+---------------------+-------------+---------+
| <=         |  Less than or Equal |	x <= y   |  False  |
+------------+---------------------+-------------+---------+

逻辑运算符

[编辑 | 编辑源代码]

下表展示了 Dragon 语言提供的逻辑运算符。假设变量 x = true,变量 y = false,则

+------------+---------------------+-------------+---------+
| Operator   | Description         | Example     | Result  |
+============+=====================+=============+=========+
| &&	     |  Logical AND        |    x && y   |  false  |
+------------+---------------------+-------------+---------+
| ||         |	Logical OR         |	x || y   |  true   |
+------------+---------------------+-------------+---------+
| !	         |  Logical Not        |	!x       |  false  |
+------------+---------------------+-------------+---------+

位运算符

[编辑 | 编辑源代码]

下表展示了 Dragon 语言提供的位运算符。假设变量 x = 8,变量 y = 2,则

+------------+-----------------------------+-------------+---------+
| Operator   | Description                 | Example     | Result  |
+============+=============================+=============+=========+
| &          |  Binary AND                 |    x & y    |  0      |
+------------+-----------------------------+-------------+---------+
| |          |	Binary OR                  |	x | y    |  10     |
+------------+-----------------------------+-------------+---------+
| ^          |  Binary XOR                 |	x ^ y    |  10     |
+------------+-----------------------------+-------------+---------+
| ~          |  Binary Ones Complement 	   |	~x       |  -9     |
+------------+-----------------------------+-------------+---------+
| <<         |  Binary Left Shift          |	x << y   |  32     | 
+------------+-----------------------------+-------------+---------+
| >>         |  Binary Right Shift         |	x >> y   |  2      |
+------------+-----------------------------+-------------+---------+

赋值运算符

[编辑 | 编辑源代码]

下表展示了 Dragon 语言提供的赋值运算符。

假设变量 x = 8,则

+------------+-----------------------------+-------------+---------+
| Operator   | Description                 | Example     | Result  |
+============+=============================+=============+=========+
| =          |  Assignment                 |    x = 8    |  x=8    |
+------------+-----------------------------+-------------+---------+
| +=         |	Add AND assignment         |	x += 5   |  x=13   |
+------------+-----------------------------+-------------+---------+
| -=	     |  Subtract AND assignment    |	x -= 3   |  x=10   |
+------------+-----------------------------+-------------+---------+
| *=         |  Multiply AND assignment    |	x *= 2   |  x=20   |
+------------+-----------------------------+-------------+---------+
| /=         |  Divide AND assignment      |	x /= 3   |  x=6    |
+------------+-----------------------------+-------------+---------+
| %=         |  Modulus AND assignment     |	x %= 2   |  x=0    | 
+------------+-----------------------------+-------------+---------+
| <<=        |	Left shift AND assignment  |	x <<= 2  |  x=0    |
+------------+-----------------------------+-------------+---------+
| >>=	     |  Right shift AND assignment |	x >>= 2  |  x=0    |
+------------+-----------------------------+-------------+---------+
| &=         |  Bitwise AND assignment     |	x &= 4   |  x=0    |
+------------+-----------------------------+-------------+---------+
| |=         |  Bitwise OR and assignment  |	x |= 3   |  x=3    |
+------------+-----------------------------+-------------+---------+
| ^=         |  Bitwise XOR and assignment |	x ^= 4   |  x=7    |
+------------+-----------------------------+-------------+---------+

其他运算符

[编辑 | 编辑源代码]
==============	======================================================================
Operator        Description
==============	======================================================================
Start:End       create list contains items from start to end
[list items]    define list items
list[index]     access list item
==============	======================================================================

运算符优先级

[编辑 | 编辑源代码]

下表展示了从最高优先级(先计算)到最低优先级的运算符。

+----------------------------------------------------------------+
| Operator                                                       |
+================================================================+
| .  []   ()     {}                                              |
+----------------------------------------------------------------+
| -  ~  :Literal  [list items]                                   |
+----------------------------------------------------------------+
| ++   --                                                        |
+----------------------------------------------------------------+
| Start:End                                                      |
+----------------------------------------------------------------+
| * /  %                                                         |
+----------------------------------------------------------------+
| + -                                                            |
+----------------------------------------------------------------+
| <<   >>                                                        |
+----------------------------------------------------------------+
| &                                                              |
+----------------------------------------------------------------+
| \|  ^	                                                         |
+----------------------------------------------------------------+
| <  >  <=  >=                                                   |
+----------------------------------------------------------------+
| =  !=                                                          |
+----------------------------------------------------------------+
| not                                                            |
+----------------------------------------------------------------+
| and   or                                                       |
+----------------------------------------------------------------+
| Assignment = += -= \*= /= %= >>= <<= &= ^= \|=                 |
+----------------------------------------------------------------+

示例

	show 3+5*4	// prints 23


华夏公益教科书