Operators are special symbols or keywords used to perform operations on values and variables. C++ provides operators for arithmetic, comparison, logical operations, assignment, bit manipulation, and more.
a + b, + is the operator and
a and b are operands.
An operator is a symbol or keyword that tells C++ to perform a specific operation.
int sum = 10 + 20;
Here, + is the addition operator.
An operand is a value or expression on which an operator works.
int result = 10 + 20;
Here, 10 and 20 are operands, while + is the operator.
Arithmetic operators are used to perform mathematical operations.
| Operator | Name | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b |
The + operator adds two values.
int a = 10;
int b = 20;
int result = a + b;
std::cout << result;
The result is 30.
The - operator subtracts one value from another.
int a = 30;
int b = 10;
int result = a - b;
The result is 20.
The * operator multiplies two values.
int price = 100;
int quantity = 5;
int total = price * quantity;
The value of total is 500.
The / operator performs division.
int a = 20;
int b = 5;
int result = a / b;
The result is 4.
The % operator gives the remainder of integer division.
int result = 17 % 5;
The result is 2.
Relational operators compare two values. The result is a Boolean value.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
The == operator checks whether two values are equal.
int a = 10;
int b = 10;
bool result = (a == b);
Here, result is true.
The != operator checks whether two values are different.
int a = 10;
int b = 20;
bool result = (a != b);
The result is true.
The > operator checks whether the left value is greater than the right value.
int age = 25;
bool result = (age > 18);
The result is true.
The < operator checks whether the left value is less than the right value.
int age = 15;
bool result = (age < 18);
The result is true.
The >= operator checks whether a value is greater than or equal to another value.
int marks = 40;
bool passed = (marks >= 40);
The result is true.
The <= operator checks whether a value is less than or equal to another value.
int age = 18;
bool result = (age <= 18);
The result is true.
Logical operators are used to combine or modify Boolean expressions.
| Operator | Name |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
The && operator returns true when both conditions are true.
int age = 25;
bool hasID = true;
bool allowed = (age >= 18 && hasID);
Both conditions must be true for allowed to be true.
The || operator returns true when at least one condition is true.
bool isStudent = true;
bool isTeacher = false;
bool allowed = isStudent || isTeacher;
The result is true because one condition is true.
The ! operator reverses a Boolean value.
bool passed = true;
bool result = !passed;
The value of result becomes false.
The = operator assigns a value to a variable.
int age = 20;
age = 25;
After the second statement, age contains 25.
Compound assignment operators combine an operation with assignment.
| Operator | Equivalent |
|---|---|
| += | a = a + b |
| -= | a = a - b |
| *= | a = a * b |
| /= | a = a / b |
| %= | a = a % b |
The increment operator ++ increases a value by one. The decrement operator -- decreases a value by one.
int count = 5;
count++;
count--;
The conditional operator ?: provides a compact way to choose between two expressions based on a condition.
int age = 20;
std::string result =
(age >= 18) ? "Adult" : "Minor";
If the condition is true, the first expression is selected; otherwise, the second expression is selected.
Bitwise operators work with the individual bits of integral values.
| Operator | Name |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left shift |
| >> | Right shift |
| Category | Examples |
|---|---|
| Arithmetic | + - * / % |
| Relational | == != > < >= <= |
| Logical | && || ! |
| Assignment | = += -= *= /= %= |
| Increment/Decrement | ++ -- |
| Conditional | ?: |
| Bitwise | & | ^ ~ << >> |
A unary operator works with one operand.
int a = 10;
a++;
The ++ operator works with one operand.
A binary operator works with two operands.
int result = 10 + 20;
The + operator works with two operands here.
#include <iostream>
int main() {
int price = 100;
int quantity = 5;
int total = price * quantity;
if (total >= 500) {
std::cout << "Large order";
} else {
std::cout << "Normal order";
}
return 0;
}
This example uses arithmetic, relational, assignment, and conditional control logic together.
#include <iostream>
int main() {
int a = 20;
int b = 6;
std::cout << "Addition: " << a + b << std::endl;
std::cout << "Subtraction: " << a - b << std::endl;
std::cout << "Multiplication: " << a * b << std::endl;
std::cout << "Division: " << a / b << std::endl;
std::cout << "Remainder: " << a % b << std::endl;
std::cout << "a == b: " << (a == b) << std::endl;
std::cout << "a > b: " << (a > b) << std::endl;
return 0;
}
Operators are essential for performing calculations, comparisons, assignments, logical operations, and other tasks in C++.
| Category | Main Operators | Purpose |
|---|---|---|
| Arithmetic | + - * / % |
Mathematical calculations |
| Relational | == != > < >= <= |
Compare values |
| Logical | && || ! |
Combine or modify conditions |
| Assignment | = += -= *= /= %= |
Assign and update values |
| Increment/Decrement | ++ -- |
Increase or decrease by one |
| Conditional | ?: |
Select between two expressions |
| Bitwise | & | ^ ~ << >> |
Operate on individual bits |
Question: Which operator is used to compare two values for equality in C++?