Operators in C++: Arithmetic, Comparison, Logical
Learn arithmetic, comparison, and logical operators in C++ to perform calculations and control program flow effectively.
In C++, operators are special symbols that perform operations on variables or constants. They are used in many areas, from arithmetic calculations to logical comparisons. In this article, we will learn the most common operator types in C++ — arithmetic, comparison, and logical — with examples.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulo (remainder) | a % b |
Simple example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "Sum: " << a + b << endl;
cout << "Difference: " << a - b << endl;
cout << "Product: " << a * b << endl;
cout << "Quotient: " << a / b << endl;
cout << "Remainder: " << a % b << endl;
return 0;
}
Output:
Sum: 13
Difference: 7
Product: 30
Quotient: 3
Remainder: 1
2. Increment and Decrement Operators
In C++, there are special operators to increase or decrease numeric variables by 1:
| Operator | Description | Example |
|---|---|---|
++ | Increment by one | x++ or ++x |
-- | Decrement by one | x-- or --x |
int x = 5;
cout << x++ << endl; // prints first, then increments → 5
cout << ++x << endl; // increments first, then prints → 7
The difference between x++ (postfix) and ++x (prefix) is especially important in loops.
3. Comparison Operators
Comparison operators compare two values and return the result as true or false.
| Operator | Description | 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 |
int a = 10, b = 20;
cout << (a == b) << endl; // 0 → false
cout << (a != b) << endl; // 1 → true
cout << (a < b) << endl; // 1 → true
When you print logical results with cout, false is shown as 0 and true as 1.
4. Logical Operators
Logical operators are used to combine multiple conditions.
| Operator | Description | Example |
|---|---|---|
&& | AND | (a > 0 && b > 0) |
|| | OR | (a > 0 || b > 0) |
! | NOT | !(a > 0) |
bool x = true;
bool y = false;
cout << (x && y) << endl; // 0 → false
cout << (x || y) << endl; // 1 → true
cout << (!x) << endl; // 0 → false
In the && (AND) operator, the result is true only if both conditions are true.
In the || (OR) operator, the result is true if at least one condition is true.
5. Example Application: Exam Result Evaluation
The example below demonstrates the combined use of arithmetic and logical operators.
#include <iostream>
using namespace std;
int main() {
int midterm = 70;
int finalExam = 80;
double average = (midterm * 0.4) + (finalExam * 0.6);
bool passed = (average >= 60) && (finalExam >= 50);
cout << "Average: " << average << endl;
cout << "Result: " << (passed ? "Passed" : "Failed") << endl;
return 0;
}
Output:
Average: 76
Result: Passed
6. TL;DR
- Arithmetic operators:
+ - * / % ++ -- - Comparison operators:
== != > < >= <= - Logical operators:
&& || ! - Operators are widely used in conditional statements and loops.
- Code can be executed in Visual Studio 2022 or GCC 11+ compilers.
In C++, operators are special symbols that perform operations on variables or constants. They are used in many areas, from arithmetic calculations to logical comparisons. This article explains the most common types of operators in C++ — arithmetic, comparison, and logical — with examples.