Page 39 - Modular_V2.0_C++_Flikpbook
P. 39
b = 5;
(a > b) ? cout<< "a is greater than b" : cout<< "b is greater than
a"; DOSBox 0.74, Cpu speed: max 100%
getch(); a is greater than b
}
OPERATOR PRECEDENCE
Operator Precedence determines the order in which operators are executed in an expression. If
an expression contains multiple operators with the same precedence, it is evaluated from left to
right or right to left, depending on the associativity of the operators. The operator precedence
and associativity in C++ is listed in the following table:
Category Operator Associativity
Postfix (), [], ->, ., ++, - - Left to right
Unary +, -, !, ~, ++, - - Right to left
Multiplicative * /, % Left to right
Additive +, - Left to right
Relational <, <=, >, >= Left to right
Equality ==, != Left to right
Logical AND && Left to right
Logical OR || Left to right
Ternary ?: Right to left
Assignment =, +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= Right to left
Comma , Left to right
EXPRESSIONS
An expression is a combination of variables, values (constants or literals), and operators that
follow the rules of the language to produce a value. Expressions can be used to perform
calculations, assign values, or control program flow.
Just like in mathematics, C++ allows the use of expressions to compute results. The value produced
by an expression can be assigned to a variable using an assignment operator. Following are
some examples of expressions:
A = 2 * (4 + 2) - 4
B = 2 + 9 / 3
Program 9: To solve expressions.
#include<iostream.h>
#include<conio.h>
int main()
{
37
Operators in C++

