Expression: An expression is a collection of constants, variables, Booleans, and operators
Divided into: arithmetic expression, string expression, assignment expression, Boolean expression, etc.
Expression and return value: Every expression returns a result, which we call return value
operator:
- arithmetic operator
- Binary operators: +, -, *, /, % (modulo), | (bitwise or), & (bitwise and), << (left shift), >> (right shift)
Unary operator: ! (reverse), ~ (complement), ++ (increase 1), -- (decrease 1)
- comparison operator
- >、<、<=、>=、==、!=
- Boolean operators
- ! (reverse), &= (assignment after AND), && (logical AND), |= (assignment after OR), || (logical OR), ^= (assignment after XOR), ?: (ternary operator ), == (equal to), ! = (not equal to)
- Congruence operator
- == is a comparison operator, === is an equality operator, that is, the value and type are the same
Examples of commonly used operators and expressions
operator | expression example | Equivalent Expression Explanation | Expression Description |
---|---|---|---|
+= | i+=1 | i=i+1 | i is incremented by 1 after that code |
-= | i-=1 | i=i-1 | i is decremented by 1 after that code |
*= | i*=2 | i=i*2 | i increase 2 times after that code |
++ | i++ | i=i-1 | (pre-increment) i is incremented by 1 after this code |
++ | ++i | i=i-1 | (Post-increment, first return the original value and then increment by 1) i increases by 1 after this code |
-- | i-- | i=i-1 | i is decremented by 1 after that code |
-- | --i | i=i-1 | i is decremented by 1 after that code |
% | i=10%3 | Remainder of 10 divided by 3 | The result of i after that code is 3 |
<= | 1<=2 | less than or equal to | true |
>= | 3>=1 | greater or equal to | true |
!= | 1!=2 | not equal to | true |
&& | true&&false | logic and | (all true is true, one false is all false) false |
|| | true||false | logical or | (one true is true, all false is false) true |
! | !true | logical NOT | false |
?: | i=true?1:2 | Ternary operator | If the result of the operand is true, the result of the expression is 1, otherwise it is 2, and the return value here is 1 |
== | 1==true | Compare | true |
=== | 1===true | congruent | false |
=== | NaN===NaN | congruent | //false* |
Please refer to the operator precedence
priority | operator | order |
---|---|---|
1 | Parentheses | () |
2 | unary operator | ++、--、! |
3 | arithmetic operator | First *, /, then +, - |
4 | relational operator | >、>=、<、<= |
5 | equality operator | ==,!=、===,!== |
6 | Logical Operators | first&&after|| |
7 | assignment operator | = |
8 | comma operator | , |