C++ Operators and Expressions

WHAT ARE OPERATORS?

The operators are the special type of functions that takes one or more parameters and gives new result. It is a symbol that tells the compiler to perform the mathematical and logical manipulations. The programming language like C or C++ is incomplete without the use of operators.

SOME OF THE BUILT IN OPERATORS ARE

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Bitwise operators

So, let us have a look all these operators briefly.

ARITHMETIC OPERATORS

The arithmetic operators are used to perform the arithmetic operations on the operands. The operations can be addition, multiplication, subtraction and division.

LET US HAVE A LOOK AT THE EXAMPLE:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
using namespace std;
 
int main()
 
{
 
int x = 5, y = 10, z;
 
//printing x and y
 
cout<<“x is:”<<x<<“and”<<“y is:”<<y;
 
z = x+y; //addition
 
cout<<“\nx+y is:”<<z;
 
z = xy; //subtraction
 
cout<<“\nx-y is:”<<z;
 
z = x*y; //multiplication
 
cout<<“\nx*y is:”<<z;
 
z = x/y; //division
 
cout<<“\nx/y is:”<<z;
 
z = x%y; //modulus
 
cout<<“\nx/ is:”<<z;
 
z = x++;
 
cout<<“\nx++ is:”<<x<<“and”<<“z is:”<<z;
 
z = x;
 
cout<<“\nx– is:”<<x<<“and”<<“z is:”<<z;
 
return 0;
 
}

Output:
 
 x is:5andy is:10
x+y is:15
x-y is:-5
x*y is:50
x/y is:0
x/ is:5
x++ is:6andz is:5
x-- is:5andz is:6

RELATIONAL OPERATORS

The relational operators are those operators that are used to compare the values of two operands. For example, by comparing two operands that their values are equal or not, or the value of one operand is greater than the other.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
 
using namespace std;
 
int main()
 
{
 
    int x=20, y=10;
 
    // greater than
 
    if (x > y)
 
        cout<<“\nx is greater than y”;
 
    else
 
    cout<<“\nx is less than or equal to y”;
 
    // greater than equal to
 
    if (x >= y)
 
        cout<<“\nx is greater than or equal to y”;
 
    else cout<<“\nx is lesser than y”;
 
    // less than
 
    if (x < y)
 
        cout<<“\nx is less than y”;
 
    else cout<<“\nx is greater than or equal to y”;
 
    // lesser than equal to
 
    if (x <= y)
 
        cout<<“\nx is lesser than or equal to y”;
 
    else cout<<“\nx is greater than y”;
 
    // equal to
 
    if (x == y)
 
        cout<<“\nx is equal to y”;
 
    else cout<<“\nx and y are not equal”;
 
    // not equal to
 
    if (x != y)
 
        cout<<“\nx is not equal to y”;
 
    else cout<<“\nx is equal y”;
 
    return (0);
 
}

Output

x is greater than y
x is greater than or equal to y
x is greater than or equal to y
x is greater than y
x and y are not equal
x is not equal to y

LOGICAL OPERATORS

The logical operators are those operators that are used to combine two or more conditions.  The logical operators are AND (&&) and OR (||). If we combine two statements using AND operator then only both the valid statements will be considered and if we combine two statements using OR operator then only either one of them will be considered.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
 
using namespace std;
 
 int main()
 
{
 
    int x = 20, y = 5, z = 10, a = 15;
 
    // logical AND
 
    if (x>y && z==a)
 
        cout<<“\nx is greater than y AND z is equal to a”;
 
    else cout<<“AND condition is not satisfied”;
 
    // logical OR
 
    if (x>y || z==a)
 
        cout<<“\nx is greater than y OR z is equal to a”;
 
    else cout<<“\nNeither x is greater than y nor z is equal to a”;
 
    // logical NOT
 
    if (!x)
 
        cout<<“\nx is zero”;
 
    else cout<<“\nx is not zero”;
 
    return (0);
 
}
Output
AND condition is not satisfied
x is greater than y OR z is equal to a
x is not zero

ASSIGNMENT OPERATORS

The assignment operators are those operators which are used to assign value to a variable. On the left side of the assignment operator the operand is a variable and the right side of the operator the operand is a value. The value on the right side of the operator should be of the same data type as of variable on the left side of the operator otherwise it will show a compile error.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
using namespace std;
int main()
 
{
 
int x = 15, y = 5;
 
//printing x and y
 
cout<<“\nx is = “<<x<<” and y is = “<< y;
 
x += y; //addition
cout<<“\nx+y is=”<< x;
 
x -= y; //subtraction
 
cout<<“\nx-y is = “<<x;
 
x *= y; //multiplication
 
cout<<“\nx*y is =”<<x;
 
x /= y; //division
 
cout<<“\nx/y is = “<< x;
 
x %= y; //modulus
 
cout<<“\nx%y is = “<< x;
 
return 0;
 
}
 
Output:
x is = 15 and y is = 5
x+y is=20
x-y is = 15
x*y is =75
x/y is = 15
x%y is = 0
 

BITWISE OPERATORS

The bitwise operators are those are used to perform bit level operations on the operands. In the bitwise operators first operators are converted to bit level and then calculation is performed on the operands. Some of the operations which are performed are addition, subtraction, multiplication, division, etc.

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
 
using namespace std;      
 
int main() {
 
   unsigned int x = 60;             // 60 = 0011 1100 
 
   unsigned int y = 13;             // 13 = 0000 1101
 
   int z = 0;          
 
   z = x & y;             // 12 = 0000 1100
 
   cout << “The value of z is : “ << z << endl ;
 
   z = x | y;             // 61 = 0011 1101
 
   cout << ” The value of z is: “ << z << endl ;
 
   z = x ^ y;             // 49 = 0011 0001
 
   cout << ” The value of z is: “ << z << endl ;
 
   z = ~x;                // -61 = 1100 0011
 
   cout << ” The value of z is: “ << z << endl ;
 
   z = x << 2;            // 240 = 1111 0000
 
   cout << ” The value of z is: “ << z << endl ;
 
   z = x >> 2;            // 15 = 0000 1111
 
   cout << ” The value of z is: “ << z << endl ;
 
   return 0;
 
}
Output :
The value of z is : 12
 The value of z is: 61
 The value of z is: 49
 The value of z is: -61
 The value of z is: 240
 The value of z is: 15

WHAT ARE EXPRESSIONS?

An expression is a sequence of operators and the operands. It is a form when you combine operands and operators.

THE EXPRESSIONS ARE OF THREE TYPES

  • Arithmetic expression
  • Relational expression
  • Logical expression

ARITHMETIC EXPRESSION

An arithmetic expression is that expression in which arithmetic operators are used. Like addition, multiplication, subtraction, division, etc.

RELATIONAL EXPRESSION

A relational expression is that expression in which relational operators are used. The operators provided in relational expression are less than (<), greater than (>), less than equal to (<=), greater than equal to (>=), etc.

LOGICAL EXPRESSION

A logical expression is that expression in which logical operators are used. Some of the logical operators are AND (&&), OR (||), NOT (!).