Operator

• Post author:
• Post category:cpp-category

Operator

The operator is a symbol that is used to perform certain operations on data. C++ provides a variety of operators. These include arithmetic operators, a relational operator, a logical operator, a bitwise operator, etc. The operators can be categorized as follows:

1. Unary Operator

A type of operator that works with one operand is known as the.
Following operators are unary operators: -,++,–
The above operators are used with one operand as follows:

-k, Z++,–x.

1. Binary Operator

A type of operator that works with two operands is known as.

+,-,*,/,%

The above operators are used with two operands follows:

```         x+y;
a+b;```

Arithmetic Operator

The arithmetic operators are the symbol that performs mathematical operations on data. C++ provides many arithmetic operators. Following is a list of all arithmetic operators in C++:

 Operation Symbol Description Addition + Adds two values Subtraction – Subtracts one value from another value Multiplication * Multiplies two values Division / Divides one value by another value Modulus % Gives the remainder of the division of two integers

Mathematical operators in C++ language

All arithmetic operators work with a numeric value. Suppose we have two variables A and B where A= 8 and B= 2. Arithmetic operators can be used on A and B as follows:

 Operation Results A+B 10 A-B 6 A*B 16 A/B 4 A%B 0

Some important points about modulus operators are followed:

• Modulus operators are also called a remainder operator.
• The modulus operator works only with integer values.
• Its modulus operator is used with the division of 0, the result will always be 0. For example, the expression 0 % 5 will give 0 as a result.
• In the expression like 3 % 5, 3 is not divisible by 5. Its result is 3.

• Working of Division Operator

The manipulation of the division operator is different from other operators. The result of the division operation is always an integer if divisor and dividend are integers. The fractional part of the quotient is truncated. For example, the result of 5.2/2.0 is 2.5 but the result of 5/2 is 2. The floating-point number must be used to get the accurate result of a division operation.

• Assignment Statement

A statement that assigns a value to a variable is known as the assignment statement. The assignment operator = is used in the assignment statement to assign a value or computational result to a variable. The name of a variable is written on the left side of the assignment operator and the value is written on the right side of an assignment operator. The value can be a constant, variable, expression or function.

Syntax

The syntax of the given assignment statement is as follows:

Variable = expression;

Variable:         It is the name of the variable to which the value is assigned.

=:                    It is the assignment operator used to assign the value to

the variable.

Expression:     It is the expression whose returned value is assigned to the variable. It can be a constant, variable or combination of operands and arithmetical operators. The expression on the right side is evaluated first. The result is then assigned to the variable on the left side.

Examples
Some examples of assignment statements are as follows

```   A =50;
C=A+B;
X=C-D+5;```
• Lvalue and Rvalue

An lvalue is an operand that can be written on the left side of assignment operators =. It must always be a single value. An rvalue is an operand that can be written on the right side of assignment operators =. All values can be used as values. But all values cannot be used as expression x = 5 is valid but the expression 5 = x is not valid.

Program:

Write a C++ sample program that performs all the mathematical operations, on two variables.

```    void main()
{
int x,z;
x=8;
y=2;
cou<<"x + y = "<<a+b<<endl;
cou<<"x - y = "<<a-b<<endl;
cou<<"x * y = "<<a*b<<endl;
cou<<"x / y = "<<a/b<<endl;
cou<<"x % y = "<<a%b<<endl;
getch();
}
```

Output:

x + y  = 10

x – y   = 6

x * y  = 16

x / y   = 4

x % y = 0

Compound assignment statement

An assignment statement that assigns a value to many variables is known as a compound assignment statement. The assignment operators = is used in this statement.

Example:

The following statement

`   A=B=10;`

Assigns the value 10 to both A and B. Some example of the compound assignment statement is as follows:

```   a = b = c = 100;
m = n = 50;```

Compound Assignment Operator

C++ language provides a compound assignment operator that combines the assignment operator with arithmetic operators. Compound assignment operators are used to perform mathematical operations more easily.

Syntax

The general syntax of the compound assignment operator is as follows:

Variable op = expression

 Variable The variable to assign a value. Op It can be an arithmetic operator. Expression It can be a constant, variables or arithmetic expressions

For example, the statement:

`  N += 15;`

Is equivalent to

`  N = N + 15;`

Both statements are equivalent and perform the same operation. Both statements add 15 in the current value of N. All another mathematical operator can also be combined with the assignment operator aucasinosonline.com.

Examples:

Some example of the compound assignment is as follows

 A += 15 is equivalent to A = A + 15 A += 15 is equivalent to A = A – 15 A *= 15 is equivalent to A = A * 15 A /= 15 is equivalent to A = A / 15 A %= 15 is equivalent to A = A % 15
Program:

Write a program that performs all compound assignment operations on an integer.

```  #include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int z;
z=15;
cout << "Value of z: "<<z<<endle;
a+=5;
cout << "Value of z after a+=5: "<<z<<endle;
a-=5;
cout << "Value of z after a-=5: "<<z<<endle;
a*=3;
cout << "Value of z after a*=5: "<<z<<endle;
a/=3;
cout << "Value of z after a/=5: "<<z<<endle;
a%=3;
cout << "Value of z after a%=5: "<<z<<endle;
a%=5;
getch();
}
return 0;
```

Output:

Value of z: 15

Value of z after a+=5: 20

Value of z after a-=5: 15

Value of z after a*3: 45

Value of z after a/3: 15

Value of z after a%3 : 0

Increment operator

The incremental operator is used to increase the value of a variable by 1. It is denoted by the symbol ++. It is a unary operator and works with a single variable.

The increment operator cannot increment the value of constants and expressions. For example, Z++ and Y++ are valid statements but 16++ is an invalid statement. Similarly, (a+b)++ or ++(a+b) are also invalid. The incremental operator can be used in two forms:

• Prefix Form
• Postfix Form
Prefix Form

In prefix form, the increment operator is written before the variable as follows:

`  ++z;`

The above line increments the value of variable z by 1.

Postfix from

In postfix form, the increment operator is written after the variable as follows:

`  z++;`

The above line also increments the value of variable z by 1.

Difference between Prefix & Postfix Increment

When increment operators used independently, prefix and postfix from work similarly. For example, the result of Z++ and ++Z is the same. But when increment operator is used in a larger expression with other operator, prefix forms work differently. For example, the result of two statements A++= ++B and A = B++ are different.

The statement A = ++B contains two operators ++ and =. It worked in the following order:

1. It increments the value of B by 1.
2. It assigns the value of B to

The above statement is equivalent to the following two statements:

```   ++B;
A = B;```

In postfix form, statement A = B++ works in the following order:

1. It assigns the value A to
2. It increments the value of B and 1.

The above statement is equivalent to the following two statements:

```   A = B;
B++;```

Program:

Write a program that explains the difference of postfix increment operator and prefix increment operator used as the independent expression.

```  #include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int a,b,x,y;
a=b=x=y = 0;
a++;
b = a;
++x;
y =x;
cout<<”a = ”<<a<<endl<<”b = ”<<b<<endl;
cout<<”x = ”<<x<<endle<<”y = ”<<y<<endl;
getch();
}
```

Output:

a = 1

b = 1

x = 1

y = 1

Program:

Write a program that explains the difference of postfix increment operator and prefix increment operator used as part of a larger expression.

```  #include <iostream>
#include <conio.h>
using namespace std;
void main()
{
Int a,b,x,y;
a = b x = y = 0;
b = a++;
y = ++x;
cout<<”a = ”<<a<<endl<<”b = ”<<b<<endl;
cout<<”x = ”<<x<<endl<<”y = ”<<y<<endl;
getch();
}
```

Output:

a = 1

b = 0

x = 1

y = 1

Decrement Operator

The decrement operator is used to decrement the value of a variable by 1. It is denoted by the symbol –. It is a unary operator and works with a single variable.

The decrement operator cannot decrement the value of constants and expressions. For example, A– and X- – are valid statements but 10–  is an invalid statement. Similarly, (a+b)- – or – -(a=b) are invalid. Decrement operator can be used in two forms:

• Prefix Form
• Postfix Form

Prefix Form

In prefix form, the decrement operator is written before the variable as follows:

`  --y;`

The above line decrements the value of variable y by 1.

Postfix Form

In postfix form, the decrement operator is written after the variable as follows:

`  y--;`

The above line also decrements the value of variable y by 1.

Different between Prefix & Postfix Decrement

When decrement operator is used independently, prefix and postfix formwork similarly. For example, the result of A- – and – – A is the same. But when increment operator is used in a larger expression with another operator, prefix and postfix forms work differently. For example, the results of two statements A = – – B and   B – – are different.

The statement A = – – B contains two operators i.e. – – and =. It works in the following order:

1. It decrements the value of B by 1.
2. It assigns the value of B to.
```   --B;
A = B;```

In postfix form, the statement A = B- – works the following order:

1. It assigns the value of B to
2. It decrements the value of B by

The above statement is equivalent to the following two statements:

```   A = B;
B--;```

Program:

Write a program that explains the difference of postfix decrement operator and prefix decrement operator used as an independent expression.

```  #include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int a,b,x,y;
a = b = x = y = 0;
a--;
b = a;
--x;
y = x;
cout<<”a = ”<<a<<endl<<”b = ”<<b<<endl;
cout<<”x = ”<<xendl<<”y = ”<<y<<endl;
getch();
}
```

Output:

a = -1

b = -1

x = -1

y = -1

Program:

Write a program that explains the difference of postfix decrement operator and prefix decrement operator used as part of larger expression.

```  #include <iostream>
#include <conio.h>
using namespace std;
void main()
{
Int a,b,x,y;
a = b x = y = 0;
b = a--;
y = --x;
cout<<”a = ”<<a<<endl<<”b = ”<<b<<endl;
cout<<”x = ”<<x<<endl<<”y = ”<<y<<endl;
getch();
}

```

Output:

a = -1

b = 0;

x = -1

y = -1

Operator Precedence

The order in which different type of operators in an expression is evaluated is known as operator precedence. It also is known as the hierarchy of operators.

Each operator has its own precedence lave. If an expression contains a different type of operator, the operators with higher precedence are evaluated before the operators with lower precedence. The order of precedence. The order of precedence in C:: language is as follows:

• Any expression given in parentheses is evaluated first.
• Then multiplication * and division/operators are evaluated.
• Then plus + and minus – operators are evaluated.
• In the case of parentheses within parentheses, the expression of the inner parentheses will be evaluated first.

Example:

The expression 10 * (24/(5 – 2)) + 13 is evaluated in the following order:

1. First, the expression of 5-2 will be evaluated. It gave a value.
2. Second, 24 will be divided by the result of the last line i.e. 24/3 giving value.
3. Thirdly 10 will be multiplied by 8e. giving a result 80.
4. Finally, 80 will be added in 13 and the last result will be.

Operator Associativity

The order in which operators of the same precedence are evaluated is known as operator associativity. If an expression contains some operators that have some precedence level, the expression is evaluated either from left-to-right or right-to-left.

Operator associativity in C++ language is as follows:

 Operator Associative ()            ++ (postfix)                  — (postfix) Left to-right + (unary)         -(unary)     ++(prefix)        –(prefix) Left to-right /                            *                               % Left to-right +                                 – Left to-right =           +=              -=                     *=               /= Right to-left

Associativity of operator

Example:

The 10* 24/5 -2+ 13 contain four operators. Two operators * and / have equal precedence. These expressions will be evaluated from left to right. Similarly, two operators + and – also have equal precedence. The expression will be evaluated as follows:

• First, of the all, the expression 10* 24 is evaluated. It gave a value 240.
• Secondly, 240 will be divided by 5 i.e. 240/5 giving a value 48.
• Thirdly, 2 will be subtracted from 48 i.e. 48-2 giving a result 46.
• Finally, 46 will be added in 13 and the last result will be 59.

Program:

Write a program that solves the following expression:

a* b / (-c * 31 % 13 ) * d

Assuming the values of variables are as following:

a = 10, b = 20, c = 15, d = 8, e = 40

```  #include <iostream>
#include <conio.h>
using namespace std;
void main()
{		int a,b,c,d,r;
a=10;
b=20;
c=15;
d=8;
r=a*b/(-c*31%13)*d;
cout<<”Result of expression is : ”<<endl;
getch();
}```

Output:

Result of expression is : -160

How above program works?

The above expression will be evaluated in the following order:

The working of the above expression in different steps will be as following:

 Step Operator Reduced expression 1 Unary – a* b / (-15 * 31 % 13 ) *d 2 * a* b / ( -465 % 13 ) * d 3 % a* b / ( -10 ) * d 4 * 200 / ( -10 ) * d 5 / -20 * d 6 * -160

Difference between C++ and C#