C++ Operators
Learn C++ operators with easy explanations, examples, and practice. Understand arithmetic, assignment, relational, logical, unary, binary, increment, and decrement operators in C++ programming.
1. What is an Operator?
An operator is a symbol used to perform an operation on one or more values or variables.
Example
int a = 10;
int b = 5;
int result = a + b;Here:
aandbare operands.+is an operator.a + bis an expression.- The result is
15.
2. What is an Expression?
An expression is a combination of values, variables, and operators that produces a result.
Examples
a + bmarks >= 50age >= 18 && age <= 60Each of these expressions produces a result.
3. Types of Operators Based on Number of Operands
Operators can be classified according to the number of operands they work with.
3.1 Unary Operators
A unary operator works with one operand.
Examples
x++;-x;!isStudent;Only one variable/value is involved.
Common Unary Operators
| Operator | Meaning | Example |
|---|---|---|
++ |
Increment | x++ |
-- |
Decrement | x-- |
- |
Negative | -x |
! |
Logical NOT | !x |
3.2 Binary Operators
A binary operator works with two operands.
Example
a + bHere:
a→ first operand+→ operatorb→ second operand
Other examples:
a - b
a * b
a > b
a == bRemember
Unary → one operand
x++;Binary → two operands
x + y;4. Types of C++ Operators
The main operators we will study are:
- Arithmetic Operators
- Assignment Operator
- Compound Assignment Operators
- Increment and Decrement Operators
- Relational Operators
- Logical Operators
5. Arithmetic Operators
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 |
Example
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; // 1Important Point: / Division Operator
The / operator is used for division.
The result depends on the data type of the operands.
1. Both Operands are int
If both operands are integers, C++ performs integer division.
int a = 10;
int b = 3;
cout << a / b;Output:
3Although mathematically:
10 ÷ 3 = 3.3333...C++ gives:
3because both a and b are int.
Important: The decimal part is removed when integer division is performed.
2. One Operand is float
If at least one operand is a float, the division produces a decimal result.
float a = 10;
int b = 3;
cout << a / b;Output:
3.33333Here:
float / int → decimal result3. Both Operands are float
float a = 10;
float b = 3;
cout << a / b;Output:
3.33333Here:
float / float → decimal result4. Using double
double can also be used for decimal calculations.
double a = 10;
double b = 3;
cout << a / b;Output:
3.33333Remember
| 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 |
Important Rule
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.
Important Point: % Remainder Operator
The % operator gives the remainder after integer division.
int a = 10;
int b = 3;
cout << a % b;Output:
1Because:
10 ÷ 3 = 3 remainder 1Therefore:
10 / 3 = 3
10 % 3 = 1Another Example
int a = 20;
int b = 6;
cout << a / b << endl;
cout << a % b << endl;Output:
3
2Because:
20 ÷ 6 = 3 remainder 2Remember
/ → gives the quotient
% → gives the remainderFor example:
17 / 5 = 3
17 % 5 = 26. Assignment Operator
The assignment operator is:
=It is used to assign a value to a variable.
Example
int age = 20;This means:
Store the value
20in the variableage.
Another example:
int x = 10;
x = 25;Now:
x = 25Important
Do not confuse:
=with:
=== means assignment.
== means comparison.
Example:
x = 10; // Assign 10 to x
x == 10; // Check whether x is 107. Compound Assignment Operators
Compound assignment operators provide a shorter way to update a variable.
Example
Instead of:
x = x + 5;we can write:
x += 5;Both have the same effect.
Common Compound Assignment Operators
| 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 |
Example
int marks = 70;
marks += 5;Now:
marks = 758. Increment Operator ++
The increment operator increases a value by 1.
int x = 5;
x++;Now:
x = 6It 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.
9. Decrement Operator --
The decrement operator decreases a value by 1.
int x = 5;
x--;Now:
x = 4It is equivalent to:
x = x - 1;There are two forms:
x--;Post-decrement
and:
--x;Pre-decrement
10. Relational Operators
Relational operators are used to compare two values.
The result of a comparison is either:
trueor:
falseRelational Operators
| 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 |
Example
int marks = 75;
cout << (marks >= 50);The expression is:
marks >= 50Since 75 is greater than 50, the result is:
true11. Logical Operators
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 |
11.1 AND Operator &&
The && operator returns true when both conditions are true.
Example
age >= 18 && marks >= 50This means:
Age must be 18 or above AND marks must be 50 or above.
11.2 OR Operator ||
The || operator returns true when at least one condition is true.
Example
day == 6 || day == 7This means:
Day is 6 OR day is 7.
11.3 NOT Operator !
The ! operator reverses a Boolean value or condition.
Example
bool isStudent = true;
cout << !isStudent;The result becomes:
falseBecause ! changes true to false and false to true.
12. Operators in if Statements
Operators are commonly used to create conditions in if statements.
Example 1
int marks = 75;
if (marks >= 50)
{
cout << "Pass";
}Here:
marks >= 50is a condition/expression using a relational operator.
Example 2
int age = 20;
int marks = 75;
if (age >= 18 && marks >= 50)
{
cout << "Eligible";
}Here:
>=→ Relational operator&&→ Logical operatorage >= 18→ First conditionmarks >= 50→ Second condition
13. Operator Summary
| 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 |
14. Quick Revision
Operator
A symbol used to perform an operation.
Operand
A value or variable on which an operator works.
Expression
A combination of values, variables, and operators that produces a result.
Unary
Works with one operand.
x++;Binary
Works with two operands.
x + y;Arithmetic
Used for calculations.
+ - * / %Relational
Used for comparison.
> < >= <= == !=Logical
Used to combine or reverse conditions.
&& || !Assignment
Used to assign a value.
=Compound Assignment
Used to update a value in a shorter way.
+= -= *= /= %=Increment / Decrement
Used to increase or decrease a value by 1.
++ --Examples
C++ Example: Prefix and Postfix Increment Operators
Question: Write a C++ program to demonstrate the difference between prefix (++a) and postfix (a++) increment operators.
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 5;
cout << "Initial values: a = " << a << ", b = " << b << endl;
// 1. Prefix increment: ++a
// The value is incremented first, then used
int prefixResult = ++a;
cout << "After prefix increment (++a): a = " << a << ", result = " << prefixResult << endl;
// 2. Postfix increment: b++
// The value is used first, then incremented
int postfixResult = b++;
cout << "After postfix increment (b++): b = " << b << ", result = " << postfixResult << endl;
return 0;
}Explanation:
-
Prefix Increment (
++a):- Increments the value of the variable before using it in an expression.
- Example:
++a→ first increaseaby 1, then use the new value.
-
Postfix Increment (
a++):- Uses the current value of the variable in an expression first, then increments it.
- Example:
a++→ use the value ofa, then increase it by 1.
-
Output Understanding:
Initial values: a = 5, b = 5 After prefix increment (++a): a = 6, result = 6 After postfix increment (b++): b = 6, result = 5
C++ Example: Arithmetic Operators
Question: Write a C++ program to demonstrate the use of arithmetic operators in C++.
#include <iostream>
using namespace std;
int main() {
int a = 15;
int b = 4;
cout << "Values: a = " << a << ", b = " << b << endl;
// 1. Addition (+)
cout << "a + b = " << a + b << endl;
// 2. Subtraction (-)
cout << "a - b = " << a - b << endl;
// 3. Multiplication (*)
cout << "a * b = " << a * b << endl;
// 4. Division (/)
cout << "a / b = " << a / b << " (integer division)" << endl;
// 5. Modulus (%) - remainder of division
cout << "a % b = " << a % b << endl;
// 6. Increment (++) - increases value by 1
a++;
cout << "After increment, a = " << a << endl;
// 7. Decrement (--) - decreases value by 1
b--;
cout << "After decrement, b = " << b << endl;
return 0;
}Explanation for Beginners:
-
Arithmetic operators are used to perform mathematical operations on numbers.
-
Operators used in the example:
+Addition-Subtraction*Multiplication/Division%Modulus (remainder)++Increment--Decrement
-
Integer division truncates the decimal part. Example:
15 / 4 = 3.
C++ Example: Assignment Operator and Arithmetic Assignment Operators
Question: Write a C++ program to demonstrate the use of assignment operators in C++.
#include <iostream>
using namespace std;
int main() {
int a = 10; // Simple assignment (=)
int b = 5;
cout << "Initial values: a = " << a << ", b = " << b << endl;
// 1. Addition assignment (+=)
a += b; // equivalent to a = a + b
cout << "After a += b, a = " << a << endl;
// 2. Subtraction assignment (-=)
a -= b; // equivalent to a = a - b
cout << "After a -= b, a = " << a << endl;
// 3. Multiplication assignment (*=)
a *= b; // equivalent to a = a * b
cout << "After a *= b, a = " << a << endl;
// 4. Division assignment (/=)
a /= b; // equivalent to a = a / b
cout << "After a /= b, a = " << a << endl;
// 5. Modulus assignment (%=)
a %= b; // equivalent to a = a % b
cout << "After a %= b, a = " << a << endl;
return 0;
}Explanation for Beginners:
-
Assignment operators are used to store values in variables and optionally perform an operation at the same time.
-
Examples:
=→ simple assignment+=→ add right value to left variable and assign result-=→ subtract right value from left variable and assign result*=→ multiply left variable by right value and assign result/=→ divide left variable by right value and assign result%=→ find remainder and assign result