Operators in C++

Previous
Next

Operators:

  • Operator is a symbol
  • Operator performs an operation on operands
  • Expression is a collection of Operators and Operands.

Operators Classified into:

  1. Unary: Performs operation on Single operand.
  2. Binary: Performs operation on 2 operands.
  3. Ternary: Performs operations on 3 operands.

Assignment operator:

  • Operator is ‘=’
  • It is used to assign value to variable.
  • Variable = Value;
  • We can assign values directly or using variables or evaluating expressions or through function return values as follows.
#include<iostream.h> 
using namespace std;
int main() 
{ 	
	int a,b,c;

	a=10 ;  /* we assign value directly */
	b=a ; /* assign value through variable */
	c=a+b ; /* assign value through expression */
	cout<<"sum is :"<<c<<endl;
	return 0;
}

Arithmetic operators:

  • Operators are : +, – , * , / , %
  • Operators are used to perform all arithmetic operations.
  • Operators follow BODMAS rule for priorities.
  • / – operator returns quotient value after division
  • % – operator returns reminder value after division
#include<iostream.h> 
using namespace std;
int main() 
{ 	
	cout<< 5+2 <<endl;
	cout<< 5-2 <<endl;
	cout<< 5*2 <<endl;
	cout<< 5/2 <<endl;
	cout<< 5%2 <<endl;
	return 0;
}

Priority of operators:

  • While evaluating the expression, we need to consider the priority
  • *, / , % operators evaluate prior than +, –
#include<iostream.h> 
using namespace std;
int main() 
{ 	
	cout<< 5+3-2 <<endl;
	cout<< 5*3%2 <<endl;
	cout<< 5+3*2 <<endl;
	cout<< (5+3)*2 <<endl;
	return 0;
}

Conditional operator:

  • It is a ternary operator. Hence it performs operation on 3 operands.
  • The operator is  ” ? : ”  
  • In Conditional operator if “Condition is true” then executes <expression1> else executes <expression2>

Syntax:

            <Condition> ? <expression1> : <expression2>

Example:

            (a>b) ? (c=25) : (c=45);

Can be written as :

            c = (a>b) ? 25 : 45;

Code:

#include<iostream.h> 
using namespace std;
int main() 
{ 	
	int a,b,c;
	cout<<"Enter 2 numbers : " <<endl;
	cin>>a>>b;
	c = (a>b) ? a : b;
	cout << "Big one is : " << c << endl;
	return 0;
}

Relational operators:

  • Operators are >, <, >=, <=, ==, !=
  • These are binary operators.
  • These operators returns a true(1) or false(0) by validating the relation among operands.
#include <iostream>
using namespace std;
int main() 
{
    int a=5,b=9;
    cout << (a > b) << endl;
    cout << (a < b) << endl;
    cout << (a == b) << endl;
    cout << (a != b) << endl;
    return 0;
}

Modifying operators:

  • Modify operators also called unary operators.
  • Modify operators are Increment and Decrement operators.
  • Increment operator increase the value of variable by 1
  • Decrement operator decrease the value of variable by 1

Increment operators is of two types
1) pre-increment
2) post-increment

Decrement operators is of two types
1) pre-decrement
2) post-decrement

#include <iostream>
using namespace std;
int main() 
{
	int a=10,b;
	b = ++a;
	cout<<"a val :"<<a << endl;
	cout<<"b val : " << b << endl;
	return 0;
}
#include <iostream>
using namespace std;
int main() 
{
	int a=10,b;
	b = a++;
	cout<<"a val :"<<a << endl;
	cout<<"b val : " << b << endl;
	return 0;
}

Logical operators:

  • These are binary operators
  • These operators return a true or false by evaluating more than 1 expression
  • Operators are
    • Logical AND (&&)
    • Logical OR (||)
    • Logical NOT (!)
  • We can find the return value of logical operator according to TRUTH TABLE
Expression1Expression2&&||!Expression1!Expression2
TrueTrueTrueTrueFalseFalse
TrueFalseFalseTrueFalseTrue
FalseTrueFalseTrueTrueFalse
FalseFalseFalseFalseTrueTrue
#include <iostream>
using namespace std;
int main() 
{
	int a=5, b=9;
	cout << ((a == 0) && (a > b)) << endl;
	cout << ((a == 0) && (a < b)) << endl;
	cout << ((a == 5) && (a > b)) << endl;
	cout << ((a == 5) && (a < b)) << endl;
	return 0;
}

Bitwise Operators:

  • Bitwise operators are used to manipulate the data at bit level.
  • They can be applied only to char and int types.
  • The operators are
    • Bitwise – AND (&)
    • Bitwise – OR (|)
    • Bitwise – XOR(^)

These operators first convert the data into corresponding binary form and then perform the operations according to truth table.

ABA&BA|BA^B
00000
01011
10011
11110
#include <iostream>
using namespace std;
int main() 
{
	int a=15,b=8,c,d,e;
	c = a & b ;
	d = a | b ;
	e = a ^ b ;
	cout <<"c val : "<< c <<endl;
	cout <<"d val : "<< d <<endl;
	cout <<"e val : "<< e <<endl;
	return 0;
}

Shift Operators:

  • The bitwise shift operators move the bit values in the memory location.
  • The left operand specifies the value to be shifted.
  • The right operand specifies the number of positions that the bits in the value are to be shifted.
  • The bit shift operators take two arguments, and looks like:
    • x << n
    • x >> n

Where x can be any kind of int variable or char variable and n can be any kind of int variable.

#include <iostream>
using namespace std;
int main() 
{
	short int a=8,b,c;
	b = a << 2 ;
	c = a >> 2 ;
	cout << "b val : " << b << endl ;
	cout << "c val : " << c << endl ;
	return 0;
}
Previous
Next

Courses Enquiry Form