Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
CPP All : Operators
Concept Notes and Resources
Concept Learning Code Sheets

Operators based on operand count #1728

Binary, unary and ternary operator classification is based on number of operands.

#include <iostream>
using namespace std;
int main()
{
int a=10;
//Binary Operators
cout<<a+a<<endl;
cout<<a-a<<endl;
cout<<a*a<<endl;
cout<<a/a<<endl;
//unary operators
cout<<a++<<endl;
cout<<a<<endl;
cout<<--a<<endl;
//ternary operator
int b=(a)?++a:--a;

cout<<b<<endl;

return 0;
}

Operators for assignment #1734

Here we demonstrate the simple assignment and its variations.

#include <iostream>
using namespace std;
int main()
{
int a;
a=10;
a+=10;
cout<<a<<endl;
a-=10;
cout<<a<<endl;
a*=10;
cout<<a<<endl;
a/=10;
cout<<a<<endl;
return 0;
}

Operators for comparison #1736

Using operators for equality, inequality, greater, lesser, greater or equal, lesser or equal types of comparisons.

#include <iostream>
using namespace std;
int main()
{
int a=10;
if(a==10) cout<<a<<endl;
if(a!=0) cout<<a<<endl;
if(a>0) cout<<a<<endl;
if(a<100) cout<<a<<endl;
if(a >=10) cout<<a<<endl;
if(a <=10) cout<<a<<endl;
return 0;
}

Operators for logical use #1739

Testing the use of logical operators like not, and and or operators.

#include <iostream>
using namespace std;
int main()
{
int a=10, b=0;
if(!b) cout<<"! tested"<<endl;
if(a && !b) cout<<"&& tested"<<endl;
if(a || b) cout<<"|| tested"<<endl;
return 0;
}

Comma as operator #1741

Comma is a sort of operator which can help form multiple instructions within same instruction.

#include <iostream>
using namespace std;
int main()
{
int x,y,z;
y=(x=5, ++x),x+=5;
z=((x=5, ++x),x+=5);
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
return 0;
}

Bitwise operators #1743

Bit-wise operators work after the given operands are converted to their binary equivalent value.

#include <iostream>
using namespace std;
int main()
{
int x=81,y=61,z;
z=x&y;
cout<<z<<endl;
z=x|y;
cout<<z<<endl;
return 0;
}

Typecasting operator #1745

Typecasting or conversion of data type is some times essentially required. This is also a kind of operator in c++

#include <iostream>
using namespace std;
int main()
{
int a=10;
float x=a/3;
cout<<x<<endl;
x=(float) a/3;
cout<<x<<endl;
return 0;
}

Typecasting while outputting #1747

Type casting is often useful for manipulating display while outputting data.

#include <iostream>
using namespace std;
int main()
{
int a=65;
cout<<(char) a<<endl;
float b=10.32;
cout<<(int) b<<endl;
return 0;
}

sizeof operator #1750

Datatype size is found by using the sizeof operator in c++

#include <iostream>
using namespace std;
int main()
{
int n=10;
int si=sizeof(n);
char c='a';
int sc=sizeof(c);
cout<<"Size of integer="<<si<<endl;
cout<<"Size of character="<<sc<<endl;
return 0;
}

Simple case of precedence #1752

The order in which operators are applied on operands in an expression can be understood with this example.

#include <iostream>
using namespace std;
int main()
{
int x=12 - 5 + 12 / 3 * 2;
cout<<x<<endl;
return 0;
}

Operator precedence with value change within expression #1784

Learning operator precedence case when value changes while evaluating the expression.

#include <iostream>
using namespace std;
int main()
{
int x=2,y=5;
int n=x - --y + y/x * (x*y++);
cout<<n<<endl;
int n1=--x - (y + y/x);
cout<<n1<<endl;
return 0;
}

Age based discount using ternary operator #2318

Solving age based discount problem with ternary (three operands) operator.

#include <iostream>
using namespace std;
int main()
{
int age=63;
int disc=age>=60?10:5;
cout<<"The discount rate is "<<disc<<" %"<<endl;
return 0;
}

Largest of three numbers using nested ternary operator #2943

A program to demonstrate nesting of ternary operator while finding the largest of three numbers.

 

#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three integers: ";
cin>>a>>b>>c;
int largest=(a>b)?(a>c?a:c):(b>c?b:c);
cout<<largest<<" is largest."<<endl;
return 0;
}

Modulus operator for integers #3589

Getting the remainder of an integer division using the modulus operator.

#include <iostream>
using namespace std;
int main()
{
int a=22,b=7;
cout<<a/b<<endl;
cout<<a%b<<endl;
return 0;
}
Solved Problems

Increment, Decrement Composite Operators Problem 1 #1756

Show the output of the following program –

Assignment, equality comparison problem #2314

Find the output of the program given below.

Output of a ternary operator operation #2321

Write output of the following program using the ternary operation logic.

Ternary operator within cout chain for selective printing #2791

Initialise a character variable. Now using the ternary operator within the co

Output Writing – Logical Operators #3215

Write the output of the following program and explain.

Practice Problem
Quizzes

Concept Notes:1  Code Sheets:14  Solved Problems:5  Practice Problems:2  Quizzes:3
Back