Ring/教程/运算符
外观
< Ring
本章将介绍 Ring 编程语言提供的运算符。
下表列出了 Ring 语言提供的所有算术运算符。假设变量 X=50,变量 Y=10,则
+------------+---------------+----------+---------+
| Operator | Description | Example | Result |
+============+===============+==========+=========+
| + | Add | x+y | 60 |
+------------+---------------+----------+---------+
| - | Subtract | x-y | 40 |
+------------+---------------+----------+---------+
| * | Multiplies | x*y | 500 |
+------------+---------------+----------+---------+
| / | Divide | x/y | 5 |
+------------+---------------+----------+---------+
| % | Modulus | x%y | 0 |
+------------+---------------+----------+---------+
| ++ | Increment | x++ | 51 |
+------------+---------------+----------+---------+
| -- | Decrement | x-- | 49 |
+------------+---------------+----------+---------+
下表列出了 Ring 语言提供的所有关系运算符。假设变量 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 |
+------------+---------------------+-------------+---------+
下表列出了 Ring 语言提供的所有逻辑运算符。假设变量 X=True,变量 Y=False,则
+------------+---------------------+-------------+---------+
| Operator | Description | Example | Result |
+============+=====================+=============+=========+
| and | Logical AND | x and y | False |
+------------+---------------------+-------------+---------+
| or | Logical OR | x or y | True |
+------------+---------------------+-------------+---------+
| not | Logical Not | not x | False |
+------------+---------------------+-------------+---------+
下表列出了 Ring 语言提供的所有位运算符。假设变量 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 |
+------------+-----------------------------+-------------+---------+
下表列出了 Ring 语言提供的所有赋值运算符。
假设变量 X=8,则
+------------+-----------------------------+-------------+---------+
| Operator | Description | Example | Result |
+============+=============================+=============+=========+
| = | Assignment | x = 10 | x=10 |
+------------+-----------------------------+-------------+---------+
| += | Add AND assignment | x += 5 | x=13 |
+------------+-----------------------------+-------------+---------+
| -= | Subtract AND assignment | x -= 3 | x=5 |
+------------+-----------------------------+-------------+---------+
| *= | Multiply AND assignment | x *= 2 | x=16 |
+------------+-----------------------------+-------------+---------+
| /= | Divide AND assignment | x /= 3 | x=2.67 |
+------------+-----------------------------+-------------+---------+
| %= | Modulus AND assignment | x %= 2 | x=0 |
+------------+-----------------------------+-------------+---------+
| <<= | Left shift AND assignment | x <<= 2 | x=32 |
+------------+-----------------------------+-------------+---------+
| >>= | Right shift AND assignment | x >>= 2 | x=2 |
+------------+-----------------------------+-------------+---------+
| &= | Bitwise AND assignment | x &= 4 | x=0 |
+------------+-----------------------------+-------------+---------+
| |= | Bitwise OR and assignment | x |= 3 | x=11 |
+------------+-----------------------------+-------------+---------+
| ^= | Bitwise XOR and assignment | x ^= 4 | x=12 |
+------------+-----------------------------+-------------+---------+
============== ======================================================================
Operator Description
============== ======================================================================
:literal using : before identifier mean literal
Start:End create list contains items from start to end
[list items] define list items
list[index] access list item
obj.name using the dot operator to access object members (attributes/methods).
obj {stmts} execute statements with direct access to object attributes & methods
func(para,...) call function using parameters separated by comma
============== ======================================================================
下表列出了运算符从高优先级(先评估)到低优先级。
+----------------------------------------------------------------+
| Operator |
+================================================================+
| . [] () {} |
+----------------------------------------------------------------+
| - ~ :Literal [list items] |
+----------------------------------------------------------------+
| ++ -- |
+----------------------------------------------------------------+
| Start:End |
+----------------------------------------------------------------+
| * / % |
+----------------------------------------------------------------+
| + - |
+----------------------------------------------------------------+
| << >> |
+----------------------------------------------------------------+
| & |
+----------------------------------------------------------------+
| \| ^ |
+----------------------------------------------------------------+
| < > <= >= |
+----------------------------------------------------------------+
| = != |
+----------------------------------------------------------------+
| not |
+----------------------------------------------------------------+
| and or |
+----------------------------------------------------------------+
| Assignment = += -= \*= /= %=>>= <<= &= ^= \|= |
+----------------------------------------------------------------+
示例
See 3+5*4 # prints 23