Documentation Index
Fetch the complete documentation index at: https://programming-for-career.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Introduction
Operators in C++ are special symbols that perform operations on variables and values. They are the building blocks of expressions and play a vital role in computations and decision-making processes.
Understanding operators is crucial for writing efficient and logical C++ programs. Let’s explore them with real-life examples!
Types of Operators in C++
C++ provides several types of operators:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Increment & Decrement Operators
- Ternary Operator
- Type Casting Operator
We’ll go through each category with real-world use cases! 💡
1. Arithmetic Operators
Used to perform basic mathematical operations.
| Operator | Meaning |
|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Remainder) |
Example:
Real-life Scenario: Calculating the total price of items in a store.
#include <iostream>
using namespace std;
int main() {
int price1 = 50, price2 = 30;
int total = price1 + price2; // Addition
cout << "Total price: " << total << endl;
return 0;
}
2. Relational (Comparison) Operators
Used to compare two values and return true or false.
| Operator | Meaning |
|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example:
Real-life Scenario: Checking if a student passed an exam.
#include <iostream>
using namespace std;
int main() {
int score = 85;
if (score >= 50) {
cout << "You passed!" << endl;
} else {
cout << "You failed." << endl;
}
return 0;
}
3. Logical Operators
Used for logical operations, often in decision-making.
| Operator | Meaning |
| -------- | ----------- |
| `&&` | Logical AND |
| `|` | Logical OR |
| `!` | Logical NOT |
Example:
Real-life Scenario: Checking if a person can vote (must be 18 or older and a citizen).
#include <iostream>
using namespace std;
int main() {
int age = 20;
bool isCitizen = true;
if (age >= 18 && isCitizen) {
cout << "You are eligible to vote." << endl;
} else {
cout << "You are not eligible to vote." << endl;
}
return 0;
}
4. Bitwise Operators
Used to manipulate bits at a binary level.
| Operator | Meaning |
| -------- | ----------- |
| `&` | Bitwise AND |
| `|` | Bitwise OR |
| `^` | Bitwise XOR |
| `<<` | Left shift |
| `>>` | Right shift |
Example:
Real-life Scenario: Checking if a number is even or odd using bitwise AND.
#include <iostream>
using namespace std;
int main() {
int num = 7;
if (num & 1) {
cout << "Odd number" << endl;
} else {
cout << "Even number" << endl;
}
return 0;
}
5. Assignment Operators
Used to assign values to variables.
| Operator | Meaning |
|---|
= | Assign |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulus and assign |
Example:
#include <iostream>
using namespace std;
int main() {
int x = 10;
x += 5; // Equivalent to x = x + 5
cout << "Updated value: " << x << endl;
return 0;
}
6. Increment & Decrement Operators
Used to increase or decrease a variable’s value by 1.
| Operator | Meaning |
|---|
++ | Increment |
-- | Decrement |
Example:
#include <iostream>
using namespace std;
int main() {
int count = 5;
count++;
cout << "New count: " << count << endl;
return 0;
}
7. Ternary Operator
A shorthand for if-else statements.
Syntax:
condition ? expression_if_true : expression_if_false;
Example:
Real-life Scenario: Checking if a number is positive or negative.
#include <iostream>
using namespace std;
int main() {
int num = -5;
string result = (num >= 0) ? "Positive" : "Negative";
cout << "Number is " << result << endl;
return 0;
}
8. Type Casting Operator
Used to convert one data type to another.
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
double result = (double)a / b; // Explicit type conversion
cout << "Result: " << result << endl;
return 0;
}
Conclusion
Operators are fundamental to C++ programming, allowing us to perform various computations and logic checks. We covered:
- Arithmetic
- Comparison
- Logical
- Bitwise
- Assignment
- Increment/Decrement
- Ternary
- Type Casting
In the next article, we’ll explore Strings in C++ and how to manipulate them efficiently!