C++ Program to Explain Operators Concept
Learn C++ step by step.
Explore blog under C++ - Operators
Jan 23, 2026
## Chapter 4: Operators
(Operators in C++)
So far we have:
- created variables
- stored data
- taken input and displayed output
Now the question arises:
How do we perform operations on these values?
This is where Operators come in.
### What are Operators? (Simple Language)
Operators are symbols
that perform some kind of operation
on variables or values.
In simple terms:
Operator = “A symbol that performs an action”
For example:
- Addition
- Subtraction
- Multiplication
- Division
### Arithmetic Operators:
These operators perform mathematical operations.
**Example: Arithmetic Operators**
C++
```
#include <iostream>
using namespace std;
int main() {
int num1 = 10;
int num2 = 3;
cout << "Addition: " << num1 + num2
<< endl;
cout << "Subtraction: " << num1 - num2
<< endl;
cout << "Multiplication: " << num1 *
num2 << endl;
cout << "Division (int): " << num1 /
num2 << endl;
cout << "Remainder: " << num1 % num2
<< endl;
return 0;
}
```
Now let's understand each operator separately:
**+ (Addition)**
C++
```
num1 + num2
```
Adds both values
Result: 13
**- (Subtraction)**
C++
```
num1 - num2
```
Subtracts the second value from the first
Result: 7
*** (Multiplication)**
C++
```
num1 * num2
```
Multiplies both values
Result: 30
**/ (Division – Integer Division)**
C++
```
num1 / num2
```
Here's a very important point:
- Both numbers are integers
- Therefore, the result will also be an integer
C++
```
10 / 3 = 3
```
The decimal part is truncated. **% (Modulus – Remainder)**
C++
```
num1 % num2
```
- After division
- The remainder is the result
C++
```
10 % 3 = 1
```
This operator is very useful in:
- even/odd checks
- loops
### Decimal Division (Float / Double)
If we need a decimal result,
at least one value
must be a float or double.
**Example: Decimal Division**
C++
```
#include <iostream>
using namespace std;
int main() {
double a = 10.0;
double b = 3.0;
cout << "Decimal division: " << a / b
<< endl;
return 0;
}
```
Output:
```
3.33333
```
**Integer Division vs Decimal Division**
| Expression | Result |
| ---------- | -------- |
| `10 / 3` |
`3` |
| `10.0 / 3` | `3.3333` |
| `10 / 3.0` | `3.3333` |
Beginner Mistakes (Important Note)
Thinking that:
C++
```
10 / 3 = 3.33
```
Correct understanding:
C++
```
int / int = int
```
### Comparison Operators:
Comparison Operators in C++
Comparison operators are used
to compare two values.
Their result is always:
- true
- or false
**Example: Comparison Operators**
C++
```
int age = 18;
age == 18; // Equal to? → true
age != 20; // Not equal to? → true
age > 16; // Greater than? → true
age < 21; // Less than? → true
age >= 18; // Greater than or equal to? → true
age <= 17; // Less than or equal to? → false
```
Understand in simple language:
- == → Are both equal?
- != → Are both different?
- > → Is it greater than?
- < → Is it less than?
- > = → Greater than or equal to
- <= → Less than or equal to
These operators are mostly
used inside if, while, and for loops.
### Logical Operators:
When we need to check more than one condition
at the same time,
we use Logical Operators.
**Example: Logical Operators**
C++
```
bool hasLicense = true;
int age = 20;
```
**AND operator (&&)**
C++
```
if (age >= 18 && hasLicense) {
cout << "You can drive!";
}
```
Meaning:
- condition 1 → age >= 18 ✔️
- condition 2 → hasLicense ✔️
The result will be true only if both are true.
**OR operator (||)**
C++
```
if (age < 18 || !hasLicense) {
cout << "You cannot drive!";
}
```
Meaning:
- age is less than 18 or
- does not have a license
If even one condition is true, the block will execute.
**NOT operator (!)**
C++
```
bool isMinor = !(age >= 18);
```
- age >= 18 → true
- !true → false
The NOT operator reverses the value.
**Summary of Logical Operators**
| Operator | Meaning |
| -------- | ------------------------- |
| `&&` | AND (Both must be true) |
| `||` | OR (At least one must be true) |
| `!` | NOT (Reverses the value) |
### Assignment Operators
Assignment operators are shortcuts for
updating the value of variables.
**Example: Assignment Operators**
C++
```
int score = 100;
score += 10; // score = 110
score -= 5; // score = 105
score *= 2; // score = 210
score /= 3; // score = 70
score %= 9; // score = 7
```
How to remember this?
C++
```
score += 10;
```
This means:
score = score + 10;
The same logic applies to other operators as well.
**Assignment Operators Table**
| Operator | Shortcut |
| -------- | ---------------- |
| `+=` | Add and assign |
| `-=` | Subtract and assign |
| `*=` | Multiply and assign |
| `/=` | Divide and assign |
| `%=` | Modulo and assign |
Beginner Mistakes (Things to keep in mind)
Don't make this mistake:
C++
```
if (age = 18) // Incorrect
```
Correct:
C++
```
if (age == 18)
```
There is a big difference between = and ==.
### Increment and Decrement:
When we need to change the value of a variable:
- by increasing it by 1
- or decreasing it by 1
we don't need to write +1 or -1 every time.
C++ provides us with special operators for this.
What do Increment (++) and Decrement (--) do?
- ++ → increases the value by 1
- -- → decreases the value by 1
**Basic Example**
C++
```
int count = 5;
count++; // Now count = 6
++count; // Now count = 7
count--; // Now count = 6
--count; // Now count = 5
```
After all four statements above,
count becomes 5 again.
**Post vs Pre — Where is the real difference?**
When ++ or --:
- appears after the variable → Post
- appears before the variable → Pre
The difference matters
when they are inside an expression.
**Post-Increment (a++)**
C++
```
int a = 5;
int b = a++;
```
This means:
1. First, b gets the old value of a → 5
2. Then, the value of a is incremented → 6
Result:
- b = 5
- a = 6
**Pre-Increment (++a)**
C++
```
int c = ++a;
```
This means:
1. First, the value of a is incremented → 7
2. Then, that new value is assigned to c
Result:
- a = 7
- c = 7
**Easy Comparison (Table)**
| Expression | What happens first | Which value is assigned |
| ---------- | ----------------- | ------------------- |
| `b = a++` | Assignment first | Old value |
| `c = ++a` | Increment first | New value |
Remember it in a real-life way:
- Post = Do the work first, then increment
- Pre = Increment first, then do the work
**Beginner Tips**
In simple statements
C++
```
count++;
++count;
```
the effect of both is the same.
Confusion arises
when the increment is inside an expression.
## Conclusion
In this article, we explored the core concepts of Explore blog under C++ - Operators. Understanding these fundamentals is crucial for any developer looking to master this topic.
## Frequently Asked Questions (FAQs)
**Q: What is Explore blog under C++ - Operators?**
A: Explore blog under C++ - Operators is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is Explore blog under C++ - Operators important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with Explore blog under C++ - Operators?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for Explore blog under C++ - Operators?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can Explore blog under C++ - Operators be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of Explore blog under C++ - Operators?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is Explore blog under C++ - Operators suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does Explore blog under C++ - Operators improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using Explore blog under C++ - Operators?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced Explore blog under C++ - Operators?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.