Learn C++ operators with easy explanations, examples, and practice. Understand arithmetic, assignment, relational, logical, unary, binary, increment, and decrement operators in C++ programming.
An operator is a symbol used to perform an operation on one or more values or variables.
int a = 10;
int b = 5;
int result = a + b;
Here:
a and b are operands.+ is an operator.a + b is an expression.15.An expression is a combination of values, variables, and operators that produces a result.
a + b
marks >= 50
age >= 18 && age <= 60
Each of these expressions produces a result.
Operators can be classified according to the number of operands they work with.
A unary operator works with one operand.
x++;
-x;
!isStudent;
Only one variable/value is involved.
| Operator | Meaning | Example |
|---|---|---|
++ |
Increment | x++ |
-- |
Decrement | x-- |
- |
Negative | -x |
! |
Logical NOT | !x |
A binary operator works with two operands.
a + b
Here:
a → first operand+ → operatorb → second operandOther examples:
a - b
a * b
a > b
a == b
Unary → one operand
x++;
Binary → two operands
x + y;
The main operators we will study are:
Arithmetic operators are used to perform mathematical calculations.
| Operator | Name | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus/Remainder | a % b |
int a = 10;
int b = 3;
cout << a + b; // 13
cout << a - b; // 7
cout << a * b; // 30
cout << a / b; // 3
cout << a % b; // 1
/ Division OperatorThe / operator is used for division.
The result depends on the data type of the operands.
intIf both operands are integers, C++ performs integer division.
int a = 10;
int b = 3;
cout << a / b;
Output:
3
Although mathematically:
10 ÷ 3 = 3.3333...
C++ gives:
3
because both a and b are int.
Important: The decimal part is removed when integer division is performed.
floatIf at least one operand is a float, the division produces a decimal result.
float a = 10;
int b = 3;
cout << a / b;
Output:
3.33333
Here:
float / int → decimal result
floatfloat a = 10;
float b = 3;
cout << a / b;
Output:
3.33333
Here:
float / float → decimal result
doubledouble can also be used for decimal calculations.
double a = 10;
double b = 3;
cout << a / b;
Output:
3.33333
| Operand Types | Example | Result |
|---|---|---|
int / int |
10 / 3 |
3 |
float / int |
10.0f / 3 |
3.33333 |
int / float |
10 / 3.0f |
3.33333 |
float / float |
10.0f / 3.0f |
3.33333 |
double / double |
10.0 / 3.0 |
3.33333 |
If both operands are
int, the result of/is integer division. If at least one operand is a floating-point type (floatordouble), the result can contain decimal values.
% Remainder OperatorThe % operator gives the remainder after integer division.
int a = 10;
int b = 3;
cout << a % b;
Output:
1
Because:
10 ÷ 3 = 3 remainder 1
Therefore:
10 / 3 = 3
10 % 3 = 1
int a = 20;
int b = 6;
cout << a / b << endl;
cout << a % b << endl;
Output:
3
2
Because:
20 ÷ 6 = 3 remainder 2
/ → gives the quotient
% → gives the remainder
For example:
17 / 5 = 3
17 % 5 = 2
The assignment operator is:
=
It is used to assign a value to a variable.
int age = 20;
This means:
Store the value
20in the variableage.
Another example:
int x = 10;
x = 25;
Now:
x = 25
Do not confuse:
=
with:
==
= means assignment.
== means comparison.
Example:
x = 10; // Assign 10 to x
x == 10; // Check whether x is 10
Compound assignment operators provide a shorter way to update a variable.
Instead of:
x = x + 5;
we can write:
x += 5;
Both have the same effect.
| Operator | Example | Equivalent To |
|---|---|---|
+= |
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 |
int marks = 70;
marks += 5;
Now:
marks = 75
++The increment operator increases a value by 1.
int x = 5;
x++;
Now:
x = 6
It is equivalent to:
x = x + 1;
There are two forms:
x++;
Post-increment
and:
++x;
Pre-increment
Both increase the value by 1. The difference becomes important when the operator is used inside a larger expression.
--The decrement operator decreases a value by 1.
int x = 5;
x--;
Now:
x = 4
It is equivalent to:
x = x - 1;
There are two forms:
x--;
Post-decrement
and:
--x;
Pre-decrement
Relational operators are used to compare two values.
The result of a comparison is either:
true
or:
false
| Operator | Meaning | Example |
|---|---|---|
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
== |
Equal to | a == b |
!= |
Not equal to | a != b |
int marks = 75;
cout << (marks >= 50);
The expression is:
marks >= 50
Since 75 is greater than 50, the result is:
true
Logical operators are used to combine or reverse conditions.
There are three main logical operators:
| Operator | Name | Meaning | ||
|---|---|---|---|---|
&& |
AND | Both conditions must be true | ||
| ` | ` | OR | At least one condition must be true | |
! |
NOT | Reverses the condition |
&&The && operator returns true when both conditions are true.
age >= 18 && marks >= 50
This means:
Age must be 18 or above AND marks must be 50 or above.
||The || operator returns true when at least one condition is true.
day == 6 || day == 7
This means:
Day is 6 OR day is 7.
!The ! operator reverses a Boolean value or condition.
bool isStudent = true;
cout << !isStudent;
The result becomes:
false
Because ! changes true to false and false to true.
if StatementsOperators are commonly used to create conditions in if statements.
int marks = 75;
if (marks >= 50)
{
cout << "Pass";
}
Here:
marks >= 50
is a condition/expression using a relational operator.
int age = 20;
int marks = 75;
if (age >= 18 && marks >= 50)
{
cout << "Eligible";
}
Here:
>= → Relational operator&& → Logical operatorage >= 18 → First conditionmarks >= 50 → Second condition| Category | Operators | Purpose | ||
|---|---|---|---|---|
| Arithmetic | + - * / % |
Mathematical calculations | ||
| Assignment | = |
Assign a value | ||
| Compound Assignment | += -= *= /= %= |
Update a variable | ||
| Increment | ++ |
Increase by 1 | ||
| Decrement | -- |
Decrease by 1 | ||
| Relational | > < >= <= == != |
Compare values | ||
| Logical | && | | ! |
Combine or reverse conditions | ||
| Unary | ++ -- - ! |
Work with one operand | ||
| Binary | + - * / > < == etc. |
Work with two operands |
A symbol used to perform an operation.
A value or variable on which an operator works.
A combination of values, variables, and operators that produces a result.
Works with one operand.
x++;
Works with two operands.
x + y;
Used for calculations.
+ - * / %
Used for comparison.
> < >= <= == !=
Used to combine or reverse conditions.
&& || !
Used to assign a value.
=
Used to update a value in a shorter way.
+= -= *= /= %=
Used to increase or decrease a value by 1.
++ --