Operators are symbols that tell C++ to perform an operation.
For example, you can use operators to:
- Add two numbers
- Subtract numbers
- Compare values
- Check conditions
- Assign values to variables
Let’s look at the most commonly used operators in C++.
Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | 10 + 5 |
- | Subtraction | 10 - 5 |
* | Multiplication | 10 * 5 |
/ | Division | 10 / 5 |
% | Remainder | 10 % 3 |
Example
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 3;
cout << "Addition: " << a + b << "\n";
cout << "Subtraction: " << a - b << "\n";
cout << "Multiplication: " << a * b << "\n";
cout << "Division: " << a / b << "\n";
cout << "Remainder: " << a % b;
return 0;
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Remainder: 1
Notice that 10 / 3 gives 3, not 3.33. This happens because both values are integers.
If you want a decimal result, use a decimal value:
double result = 10.0 / 3;
Assignment Operator
The assignment operator = is used to assign a value to a variable.
int age = 14;
Here, 14 is assigned to age.
You can also change the value:
int age = 14;
age = 15;
Now age contains 15.
Comparison Operators
Comparison operators are used to compare two values.
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
These operators produce either true or false.
For example:
#include <iostream>
using namespace std;
int main() {
int age = 14;
cout << (age > 10);
return 0;
}
Output:
1
1 means true.
We will use comparison operators more often when we learn if-else statements.
Logical Operators
Logical operators are used to combine or reverse conditions.
| Operator | Meaning |
|---|---|
&& | AND |
|| | OR |
! | NOT |
AND &&
Both conditions must be true.
int age = 14;
cout << (age > 10 && age < 18);
Both conditions are true, so the result is true.
OR ||
At least one condition must be true.
int age = 20;
cout << (age < 18 || age > 18);
The second condition is true, so the result is true.
NOT !
The NOT operator reverses a value.
bool passed = true;
cout << !passed;
Output:
0
Because true becomes false.
Increment and Decrement Operators
The increment operator ++ increases a value by 1.
int number = 5;
number++;
cout << number;
Output:
6
The decrement operator -- decreases a value by 1.
int number = 5;
number--;
cout << number;
Output:
4
These operators are commonly used with loops.
Compound Assignment Operators
C++ also provides shorter ways to perform calculations and assign the result.
| Operator | Example | Same As |
|---|---|---|
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
Example:
#include <iostream>
using namespace std;
int main() {
int number = 10;
number += 5;
cout << number;
return 0;
}
Output:
15
A Simple Calculator Example
Let’s use operators to create a basic calculator.
#include <iostream>
using namespace std;
int main() {
int a = 20;
int b = 5;
cout << "Addition: " << a + b << "\n";
cout << "Subtraction: " << a - b << "\n";
cout << "Multiplication: " << a * b << "\n";
cout << "Division: " << a / b;
return 0;
}
Output:
Addition: 25
Subtraction: 15
Multiplication: 100
Division: 4
Key Points to Remember
- Operators are used to perform operations in C++.
- Arithmetic operators perform calculations.
=assigns a value to a variable.- Comparison operators compare values.
- Logical operators work with conditions.
++increases a value by1.--decreases a value by1.- Compound assignment operators provide shorter ways to update values.
Mini Quiz
Mini Project
Mini Project: Shopping Bill Calculator
Question:
Write a C++ program that calculates the bill for items purchased by a customer.
- Store the prices of three items.
- Store the quantity purchased for each item.
- Calculate the cost of each item using multiplication.
- Calculate the total bill using addition.
- Apply a discount using subtraction.
- Use a comparison operator to check whether the final bill is greater than
1000. - Use a compound assignment operator to update the bill.
- Display the item costs, final bill, and comparison result.
View Answer Code
#include <iostream>
using namespace std;
int main() {
double price1 = 250;
double price2 = 300;
double price3 = 150;
int quantity1 = 2;
int quantity2 = 1;
int quantity3 = 3;
double cost1 = price1 * quantity1;
double cost2 = price2 * quantity2;
double cost3 = price3 * quantity3;
double total = cost1 + cost2 + cost3;
total -= 100;
cout << "Item 1 Cost: " << cost1 << "\n";
cout << "Item 2 Cost: " << cost2 << "\n";
cout << "Item 3 Cost: " << cost3 << "\n";
cout << "Final Bill: " << total << "\n";
cout << "Bill Greater Than 1000: " << (total > 1000);
return 0;
}
What’s Next?
In the next chapter, we will learn C++ Conditional Statements and see how a program can make decisions using if, else, and switch.
Frequently Asked Questions (FAQs)
Q1. What are C++ Operators?
C++ Operators are symbols used to perform operations such as calculations, comparisons, assignments, and logical operations in a program.
Q2. What are C++ arithmetic operators?
C++ arithmetic operators are used for mathematical calculations. They include +, -, *, /, and %.
Q3. What are C++ comparison operators used for?
C++ comparison operators compare two values and produce either true or false. Examples include ==, !=, >, <, >=, and <=.
Q4. What are C++ logical operators?
C++ logical operators are used to combine or reverse conditions. The main logical operators are && (AND), || (OR), and ! (NOT).
Q5. What is the assignment operator in C++?
The C++ assignment operator = is used to assign a value to a variable. For example, int age = 14; assigns 14 to age.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
