X = 5 + (7 % 2) // x = 6 (same as without parenthesis)įrom greatest to smallest priority, C++ operators are evaluated in the following order: Basically, it returns the opposite Boolean value of evaluating its operand. It has only one operand, to its right, and inverts it, producing false if its operand is true, and true if its operand is false. The operator ! is the C++ operator for the Boolean operation NOT. Therefore, in the last expression ( (b=2) = a), we first assigned the value 2 to b and then we compared it to a (that also stores the value 2), yielding true. (b+4 > a*c) // evaluates to false, since (3+4 > 2*6) is falseīe careful! The assignment operator (operator =, with one equal sign) is not the same as the equality comparison operator (operator =, with two equal signs) the first one ( =) assigns the value on the right-hand to the variable on its left, while the other ( =) compares whether the values on both sides of the operator are equal. (a*b >= c) // evaluates to true, since (2*3 >= 6) is true (a = 5) // evaluates to false, since a is not equal to 5 Relational and comparison operators ( =, !=, >, =, While in Example 2, it is the value x had before being increased. In Example 1, the value assigned to y is the value of x after being increased. On the other hand, in case that it is used as a suffix ( x++), the value is also increased, but the expression evaluates to the value that x had before being increased. Although in simple expressions like x++ or ++x, both have exactly the same meaning in other expressions in which the result of the increment or decrement operation is evaluated, they may have an important difference in their meaning: In the case that the increase operator is used as a prefix ( ++x) of the value, the expression evaluates to the final value of x, once it is already increased. That means that it can be written either before the variable name ( ++x) or after it ( x++).
X 1 2 X 5 CODE
Nowadays, this type of code optimization is generally performed automatically by the compiler, thus the three expressions should produce exactly the same executable code.Ī peculiarity of this operator is that it can be used both as a prefix and as a suffix. In the early C compilers, the three previous expressions may have produced different executable code depending on which one was used. Are all equivalent in its functionality the three of them increase by one the value of x.