Arithmetic and Relational Operators in C++ tutorial
Arithmetic Relational Operars in C++ cplus
Introduction
Operators are symbols that tell the compiler to perform specific operations on variables and values. Arithmetic operators handle mathematical operations, while relational operators are used to compare values.
These are used to perform basic mathematical operations.
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition |
5 + 3 |
8 |
- |
Subtraction |
5 - 3 |
2 |
* |
Multiplication |
5 * 3 |
15 |
/ |
Division |
6 / 3 |
2 |
% |
Modulus (remainder) |
5 % 3 |
2 |
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "a + b = " << a + 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;
}
These are used to compare two values and return a boolean (true or false).
| Operator | Description | Example | Result |
|---|---|---|---|
== |
Equal to |
5 == 3 |
false |
!= |
Not equal to |
5 != 3 |
true |
> |
Greater than |
5 > 3 |
true |
< |
Less than |
5 < 3 |
false |
>= |
Greater than or equal to |
5 >= 3 |
true |
<= |
Less than or equal to |
5 <= 3 |
false |
Example:
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 3;
cout << (x == y) << endl; // 0
cout << (x != y) << endl; // 1
cout << (x > y) << endl; // 1
cout << (x < y) << endl; // 0
cout << (x >= y) << endl; // 1
cout << (x <= y) << endl; // 0
return 0;
}
Output:
0
1
1
0
1
0
Arithmetic operators perform basic mathematical operations.
Relational operators compare values and return a boolean result.
They are crucial for building expressions, conditions, and control structures in C++.