# Operators

• Operators are function which operates another function.

• There are many types of operators in C++.

• They are explained in detail in this chapter.

# Arithmetic Operators

• There are many operators used in C++.

• The first operator type is arithmetic operator.

• They perform five basic operations.

• They are listed in the below table.

 Operator Definition Example + Addition c= a+b - Subtraction c=a -b * Multiplication c= a* b / Division c= a/b % modulus c=a%b

Program 1: To perform arithmetic operation

#include <iostream.h>

int main()

{

using namespace std;

float a, b;

cout << "Enter a number: ";

cin >> a;

cout << "Enter another number: ";

cin >> b;

cout << "a = " << a << "; b = " << b << endl;

cout << "a+b = " << a+b << endl;

cout << "a-b = " << a-b << endl;

cout << "a*b = " << a*b << endl;

cout << "a/b = " << a/b << endl;

return 0;

}

 Note: In the above example the main function is declared as int, hence the return statement is used. Instead void can be used to declare main, so that it is not necessary to use return statement.

# Logical Operator

• These operators are used to check the condition between two values.

• There are three different types of operators available in this.

• They are listed below in the table.

 Operator Definition Example && AND operator a >b && a>c || OR operator a> b || a > c ! NOT operator !(a=b)

Program 2: To perform logical and operation.

#include<iostream.h>

#include<conio.h>

void main()

{

int a,b,c;

cout << " Enter the value for a,b,c";

cin>>a>>b>>c;

if((a>b) && (a>c))

cout<<"a is greatest"<<a<<endl;

else if ((b>a)&&(b>c))

cout<<" b is greater"<<b<<endl;

else

cout<<"c is greatest"<<c<<endl;

getch();

}

Program 3: To perform logical or operation.

#include<iostream.h>

#include<conio.h>

void main()

{

int a,b,c;

cout << " Enter the value for a,b,c";

cin>>a>>b>>c;

if((a>b) || (a=b))

cout<<"a is greatest"<<a<<endl;

else if ((b>a)&&(b=c))

cout<<" b is greater"<<b<<endl;

else

cout<<"c is greater"<<c<<endl;

getch();

}

# Relational Operator

• These operators are used to compare two operands to check whether the given value is greater than, less than, greater than or equal to or less than equal to the other.

• The following table gives the list of relational operators

 Operator Definition Example == Equal to a==b != Not Equal to a != b > Greater than a > b < Less than a < b >= Greater than or equal to a >= b <= Less than or equal to a < = b

Here there are some examples:

Program 4: To find the smallest among the given numbers using relational operator

#include<iostream.h>

#include<conio.h>

void main()

{

int a,b,c;

cout << " Enter the value for a,b,c";

cin>>a>>b>>c;

if((a<b) && (a<c))

cout<<"a is smaller"<<a<<endl;

else if ((b< a)&&(b<c))

cout<<" b is smaller"<<b<<endl;

else

cout<<"c is smaller"<<c<<endl;

getch();

}

 Note: Be careful the operator = (one equal to sign) is not the same as the operator == (double equal to sign). The first one is an assignment operator and the other one is the equality operator that compares whether both expressions in the two sides of it are equal to each other or not.

# Increment / Decrement Operator (++, --)

• Possibly the most well known operator in C++ is a unary operator than can be applied to variables and increments the value the hold

• In c++ there are two important operators.

• They are increment and decrement operator

• The following table shows the operators in detail

 Operator Definition Example ++ Increment A++ -- Decrement A--

Program 5: To perform increment operation

#include <iostream.h>

using namespace std;

int main()

{

int i = 5;

cout << "++i = " << i << endl;

i++;

cout<<"i=="<< i << endl;

getch();

}

Program 6: To perform decrement operation

#include <iostream.h>

using namespace std;

int main()

{

int i = 5;

cout << "--i = " << i << endl;

i--;

cout<<"i=="<< i << endl;

getch();

}

 Note: The values can also be increment or decremented by other values. For example: a+2; this adds two to each value of a.

# Conditional Operator

• These operators are used to store a value depending upon a condition.
• This is also called as ternary operator.
• It uses two symbol (? :) question mark and colon.
• The conditional operator works with the following rules.
• βThe first operand is implicitly converted to Boolean.
• It is executed and all side effects are completed before continuing.
• The first operand evaluates true, if the second operand is evaluated.
• The first operand evaluates false, the third operand is evaluated.
• Syntax
 Conditional expression1? Expression 2: expression 3
• For example
 a > b? a: b

In the above example if a is greater than b then the first value a is printed else the second value b is printed.

Program 7: A program using conditional operator

#include <iostream.h>

using namespace std;

int main() {

int a= 1, b = 2;

cout << ( a > b ? a : b ) << " is greater." << endl;

}

# Size of Operator

• This operator is used to get the size of its operand with respect to the size of type char.

• It is a unary operator.

• The syntax is

 size of (type name)
Where type name: type of C++ data type.

# Comma Operator

• The comma operator is used to separate two or more expression.
• It included where only one expression is expected.
• The expression using comma operator is evaluated from left to right.
• For example
• a= (b =2,b + 7);
 Note: The sizeof operator cannot be used in the following operands. They are functions, bit fields, void, dynamic arrays and in incomplete parenthesized names of incomplete type.

# Operator Precedence

• Operator precedence represents the order in which operators are to be used.
• Each operator is given precedence from high to low.
• Each operator is executed according to its precedence.
• Precedence depends on the priority of each operator according to the operating system.
• The following table shows the order of precedence among the operators.
 Operator Name ! Boolean Not * Multiplication / Division % Modulus + Addition - Subtraction < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to != Not equal to && Logical And || Logical Or = Assign *= Multiply and assign /= Divide and assign %= Modulus and assign += Add and assign -= Subtract and assign