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
Parameters and return value of pow()
Type double, due to rounding errors in floating point numbers, the result may not be precise.
Incrementing and decrementing
Adding 1 to and subtracting 1 from
Prefix increment/decrement
If we use it the operand is incremented/decremented and the expression evaluates to the value of the operand
Postfix increment/decrement
A copy of the opedand is made, yhe operand is incremented/decremented and the copy is evaluated
Side effects
If a function or expression does anithing that persists beyond the life of the function/ expression itself
Common side effects
Changing the value of objects, doing input or output or updating a graphical user interface
Comma operator
Allows to evaluate multiple expressions when a single expression is allowed, evaluates the left then the right operand and returns the result of the right
Other use of comma
As a seperator, it doesn’t invoke the comma operator
Conditional operator ?:
Ternary operator, provides a shorthand method for doing particular type of if/else statement
Paranthesization of the conditional operator
Conditional operator has low precidence, so it is common convention to paranthesize at least the conditional part of the operator
Evaluation of the conditional operator
As an expression, must match the type of both expressions or the second expression must be convertable to the type of the first
Use of conditional operator
- ehen we need a conditional initializer or assignment for a variable
- to pass conditional value to a function
- simple conditionals
- do Not use for complex if/else statements
Relational operators
Operators that let you compare two values, it evaluates to a boolean value
Comparison and equality with floating point
Comparison can be done but it can’t be too precise, equality should be avoided.
Floating point equality
Can be done by using a function that looks to see if two numbers are almost the same
Epsilon
Small positive number, value that represents “close enough”
std::abs()
Function in the header that returns the absolute value of its argument *downside-everytime we call the function we have to pick an epsilon appropriate to the input
Donald knut method
Epsilon is relative to a or b, it is calculated by algorithams choice of the larger a or b and multiplies it by epsilon in percents
Inequality
Call the function and use logical NOT operator
Approximately equal()
Will work in most cases, but not well when the numbers aproach zero
Logical operators
To test multiple conditions
C++ logical operators
Logical NOT!, OR||, AND&&
Logical not
Flips a boolean value from true to false and vice versa
Logical not with a result of operators
The operators and operands need to be in parenthesis
Logical or
Used to test whether either of two conditions is true, if either or both are true it returns true
Logical and
Used to test whether both operands are true
Short circut evaluation
Used for optimization, when operator returns a value only on the basis of the evaluation of the first operand
Mixing logical and and or
And has higher precidence so it will evaluate first unles parenthesized, also evaluates from right to left
De Morgan’s law
Distribution of logical not can't be done like *!(x&&y)=!x&&!y You must flip logical Not and And like *!(x&&y)=!x||!y *!(x||y)=!x&&!y
Logical exclusive XOR operator
Not in c++, it can be mimicked with logical not operator
It is used to test whether odd number of conditions are true, if we don’t work with boolean we can static_cast them into boolean.