十一、操作符的次序
Table 3.6. 操作符次序 操作符 描述
++, -- 自增,自减 -, ~, ! 单目 ** 乘方 =~, !~ 模式匹配 *, /, %, x 乘,除,取余,重复 +, -, . 加,减,联接 <<, >> 移位 -e, -r, etc. 文件状态 <, <=, >, >=, lt, le, gt, ge 不等比较 ==, !=, <=>, eq, ne, cmp 相等比较 & 位与 |, ^ 位或,位异或 && 逻辑与 || 逻辑或 .. 列表范围 ? and : 条件操作符 =, +=, -=, *=, 赋值 and so on , 逗号操作符 not Low-precedence logical NOT and Low-precedence logical AND or, xor Low-precedence logical OR and XOR
.操作符结合性(associativity):
Table 3.7. 操作符结合性
操作符 结合性 ++, -- 无 -, ~, ! Right-to-left ** Right-to-left =~, !~ Left-to-right *, /, %, x Left-to-right +, -, . Left-to-right <<, >> Left-to-right -e, -r, 无 <, <=, >, >=, lt, le, gt, ge Left-to-right ==, !=, <=>, eq, ne, cmp Left-to-right & Left-to-right |, ^ Left-to-right && Left-to-right || Left-to-right .. Left-to-right ? and : Right-to-left =, +=, -=, *=, Right-to-left and so on , Left-to-right not Left-to-right and Left-to-right or, xor Left-to-right
建议: 1、当你不确定某操作符是否先执行时,一定要用括号明确之。 2、用多行、空格等方式提高程序的可读性。
|