Page 31 - Modular_V1.1_Flipbook
P. 31
Logical OR || Left to right
Ternary ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right
EXPRESSIONS
Similar to Mathematics, C++ also allows you to use expressions. An expression is a combination
of variables or values with operators which are arranged according to the rules of the language.
Every expression produces some value which is assigned to the variable with the help of 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() {
int a = 6;
int b = 4;
int c = 2;
int result;
clrscr();
cout<<”Result of the expression is: “ << a - b + c << “\n”;
cout<<”Result of the expression is: “ << a + b / c << “\n”;
cout<<”Result of the expression is: “ << (a + b) / c << “\n”;
getch();
}
TYPE CASTING
Type casting means to convert the value of one data type into another data type. For example,
we can convert an integer value into float type. There are two types of type casting in C++:
implicit and explicit.
Implicit Type Casting
Implicit type casting is also known as automatic type casting. It is done by the C++ compiler. For
example, a char data type is automatically converted into int by C++ compiler.
char i = ‘A’;
int num;
num = i;
If you run the preceding code, you will get the 65 as output. Because, the ASCII (American
Standard Code for Information Interchange) value of the character A is 65.
Operators in C++ 29

