1.
Arithmetic Operators
These operators can operate on any arithmetic operations in C++. Let's have a look at an example
#include <iostream>
using namespace std;
main() {
int a = 21;
int b = 10;
int c ;
c = a + b;
cout << "Line 1 - Value of c is :" << c << endl ;
c = a - b;
cout << "Line 2 - Value of c is :" << c << endl ;
c = a * b;
cout << "Line 3 - Value of c is :" << c << endl ;
c = a / b;
cout << "Line 4 - Value of c is :" << c << endl ;
c = a % b;
cout << "Line 5 - Value of c is :" << c << endl ;
return 0;
}
2, Compound assignment operators
1. #include <iostream>
int main() {
int a = 10;
int b = 5;
std::cout << "Initial values: a = " << a << ",
b = " << b << std::endl;
// Addition assignment
a += b; // a = a + b
std::cout << "a += b: a = " << a << std::endl;
// Subtraction assignment
a -= b; // a = a - b
std::cout << "a -= b: a = " << a << std::endl;
// Multiplication assignment
a *= b; // a = a * b
stdenda::cout << "a *= b: a = " << a <<
std::endl;
// Division assignment
a /= b; // a = a / b
std::cout << "a /= b: a = " << a << std::endl;
// Modulus assignment
a %= b; // a = a % b
std::cout << "a %= b: a = " << a << std::endl;
return 0;
}
2.
3, the Relational Operators
// CPP Program to demonstrate the Relational Operators
#include <iostream>
using namespace std;
int main()
{
int a = 6, b = 4;
// Equal to operator
cout << "a == b is " << (a == b) << endl;
// Greater than operator
cout << "a > b is " << (a > b) << endl;
// Greater than or Equal to operator
cout << "a >= b is " << (a >= b) << endl;
// Lesser than operator
cout << "a < b is " << (a < b) << endl;
// Lesser than or Equal to operator
cout << "a <= b is " << (a <= b) << endl;
// true
cout << "a != b is " << (a != b) << endl;
return 0;
}
4. Logical Operators(!, &&, ||)
// CPP Program to demonstrate the Logical Operators
#include <iostream>
using namespace std;
int main()
{
int a = 6, b = 4;
// Logical AND operator
cout << "a && b is " << (a && b) << endl;
// Logical OR operator
cout << "a || b is " << (a || b) << endl;
// Logical NOT operator
cout << "!b is " << (!b) << endl;
return 0;
}
5. Increment/Decrement Operators: (++) and (--)
When an increment or decrement is used as part of an expression, there is an important
difference in prefix and postfix forms. If you are using prefix form then increment or decrement
will be done before rest of the expression, and if you are using postfix form, then increment or
decrement will be done after the complete expression is evaluated.
#include <iostream>
using namespace std;
main() {
int a = 21;
int c ;
// Value of a will not be increased before assignment.
c = a++;
cout << "Line 1 - Value of a++ is :" << c << endl ;
// After expression value of a is increased
cout << "Line 2 - Value of a is :" << a << endl ;
// Value of a will be increased before assignment.
c = ++a;
cout << "Line 3 - Value of ++a is :" << c << endl ;
return 0;
}