Operators are symbols that tell the compiler to perform specific mathematical or logical manipulations. In C++, we group them into three main categories that you’ll use constantly.
1. Arithmetic Operators
These are used for basic math.
OperatorName DescriptionExample (a=10,b=3)
+ AdditionAdds two operands a + b = 13
- SubtractionSubtracts second from first a - b = 7
* MultiplicationMultiplies two operands a * b = 30
/ DivisionDivides first by second a / b = 3 (Integer division)
% ModuloReturns the remainder a \% b = 1$
Note on Modulo (
%): This is incredibly useful for checking if a number is even or odd (e.g.,num % 2 == 0).
2. Relational (Comparison) Operators
Used to compare two values. They always return a Boolean (true or false).
==(Equal to) — Don't confuse with=which assigns a value!!=(Not equal to)>(Greater than) /<(Less than)>=(Greater than or equal to) /<=(Less than or equal to)
3. Logical Operators
Used to combine multiple conditions.
&&(AND): True only if both conditions are true.||(OR): True if at least one condition is true.!(NOT): Reverses the result (True becomes False).
#include <iostream>
using namespace std;
int main() {
double price = 100.0;
int quantity = 3;
double taxRate = 0.08; // 8% tax
bool isMember = true;
// 1. Arithmetic: Calculate subtotal
double subtotal = price * quantity;
// 2. Arithmetic: Calculate tax
double totalTax = subtotal * taxRate;
double finalPrice = subtotal + totalTax;
// 3. Relational & Logical: Check for discount eligibility
// Eligible if spending > 200 AND they are a member
bool getsDiscount = (finalPrice > 200.0) && isMember;
cout << "Subtotal: $" << subtotal << endl;
cout << "Total with Tax: $" << finalPrice << endl;
cout << "Discount Eligible: " << (getsDiscount ? "Yes" : "No") << endl;
return 0;
}