Operators And Its Precedence Flashcards
Operator’s precedence
The order in which the operators are evaluated in a compound expression
Precedence
All operators in C++ are assigned a level of precedence, operators with higher level are evaluated first
Operator associativity
It tells the compiler the direction of evaluation of operators that are assigned the same level of precedence
Precedence levels
1 is highest, 17 is lowest
Parenthesis
Used to make it clear how the expression should be evaluated, even if they are unneccesary
Unary arithmetic operators
Plus and minus, should be placed immediately preceding the operand
Unary minus
Returns the operand multiplied by -1
Unary plus
Returns the value of the operand
Binary arithmetic operators
Addition, subtraction, multiplication, division and modulus (remainder%)
Floating point division
Occurs when either of the operands are floating poating point values, returns a floating point value and the fraction is kept
Integer division
If both of the operands are integers it drops fractions and returns an integer value.
Using static_cast<> to do floating point division with integers
Use static_cast<> to convert an integer to floating point number
Dividing by zero
Will generaly cause the program to crash
Arithmetic assignment operators
Assignment, additional assignment, subtraction assignment, multiplication assignment, division assignment and modulus assignment
Modulus operator
Operator that returns the ramainder after doing an integer division, only works with integer operands
Use for modulus
For testing whether a number is evenly divisable by another number
Modulus with negative numbers
Returns result with the sign of x (x/y)
Exponent operator (^)
Bitwise XOR operator
Exponent in C++
C++ doesn’t include exponent operator, exponents are done with #include the header and using the pow() function