C++ Operators: Arithmetic & Relational (Part 1)
Track completion, mastery, and revision.
Operators are symbols used to perform specific mathematical, logical, or assignment operations in a program. They act on operands (variables or values) to produce a desired result.
C++ provides a rich set of operators to perform various operations. These operators can be broadly categorized into:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Misc Operators
In this guide, we will focus on the first three: Arithmetic, Assignment, and Relational Operators.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical calculations.
To understand how they work, let's assume two variables: A = 10 and B = 20.
| Operator | Description | Example (A = 10, B = 20) |
| :--- | :--- | :--- |
| + | Adds two operands | A + B results in 30 |
| - | Subtracts the second operand from the first | A - B results in -10 |
| * | Multiplies both operands | A * B results in 200 |
| / | Divides the numerator by the denominator (integer division discards fractions) | B / A results in 2 |
| % | Modulus operator; returns the remainder after integer division | B % A results in 0 |
| ++ | Increment operator; increases the integer value by one | A++ results in 11 (post-increment) |
| -- | Decrement operator; decreases the integer value by one | A-- results in 9 (post-decrement) |
Unary vs. Binary Operators
- Unary Operators: Operators that require only one operand to operate (e.g.,
++and--). - Binary Operators: Operators that require two operands to operate (e.g.,
+,-,*,/,%).
Code Example: Arithmetic Operators
The following standard C++ program demonstrates all the arithmetic operators in action. Note the use of postfix increment (a++) and decrement (b--) operators.
#include <iostream>
int main() {
int a = 21;
int b = 10;
int c;
c = a + b;
std::cout << "a + b = " << c << std::endl;
c = a - b;
std::cout << "a - b = " << c << std::endl;
c = a * b;
std::cout << "a * b = " << c << std::endl;
c = a / b; // Integer division
std::cout << "a / b = " << c << std::endl;
c = a % b; // Remainder
std::cout << "a % b = " << c << std::endl;
// Post-increment: 'c' is assigned the current value of 'a' (21), then 'a' is incremented to 22
c = a++;
std::cout << "Value of c after c = a++ is " << c << std::endl;
std::cout << "Actual value of a now is " << a << std::endl;
// Post-decrement: 'c' is assigned the current value of 'b' (10), then 'b' is decremented to 9
c = b--;
std::cout << "Value of c after c = b-- is " << c << std::endl;
std::cout << "Actual value of b now is " << b << std::endl;
return 0;
}
Output:
a + b = 31
a - b = 11
a * b = 210
a / b = 2
a % b = 1
Value of c after c = a++ is 21
Actual value of a now is 22
Value of c after c = b-- is 10
Actual value of b now is 9
Assignment Operators
Assignment operators are used to assign values to variables. C++ supports the standard assignment operator (=) as well as compound (shorthand) assignment operators.
| Operator | Description | Example | Equivalent To |
| :--- | :--- | :--- | :--- |
| = | Simple Assignment: Assigns values from the right side to the left side variable. | C = A + B | C = A + B |
| += | Add and Assign: Adds the right operand to the left operand and assigns the result to the left operand. | C += A | C = C + A |
| -= | Subtract and Assign: Subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A | C = C - A |
| *= | Multiply and Assign: Multiplies the left operand by the right operand and assigns the result to the left operand. | C *= A | C = C * A |
| /= | Divide and Assign: Divides the left operand by the right operand and assigns the result to the left operand. | C /= A | C = C / A |
| %= | Modulus and Assign: Takes the modulus using two operands and assigns the remainder to the left operand. | C %= A | C = C % A |
Relational Operators
Relational operators are used to compare two values. They evaluate a condition and return a boolean value: either true (1) or false (0).
Assume A = 10 and B = 20:
| Operator | Description | Example (A = 10, B = 20) | Result |
| :--- | :--- | :--- | :--- |
| == | Checks if the values of two operands are equal. | A == B | false |
| != | Checks if the values of two operands are not equal. | A != B | true |
| > | Checks if the left operand is greater than the right operand. | A > B | false |
| < | Checks if the left operand is less than the right operand. | A < B | true |
| >= | Checks if the left operand is greater than or equal to the right operand. | A >= B | false |
| <= | Checks if the left operand is less than or equal to the right operand. | A <= B | true |
Finished reading?