An operator is a symbol that performs an operation on one or more variables and values.
You have already learned about the output operator (>>). Another important operator is the assignment operator (=), which assigns a value to a variable.
int x;
x = 10; // Assigning the value 10 to x
10) is assigned to the variable on the left (x).In addition to the assignment operator, there are six important integer arithmetic operators in C++, which are listed in the table below:
| Operators | Functionality |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (Remainder) |
| – | Negation (Unary minus) |
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 3;
cout << a << " + " << b << " = " << (a + b) << endl;
cout << a << " - " << b << " = " << (a - b) << endl;
cout << a << " * " << b << " = " << (a * b) << endl;
cout << a << " / " << b << " = " << (a / b) << endl;
cout << a << " % " << b << " = " << (a % b) << endl;
return 0;
}
5 + 3 = 8
5 - 3 = 2
5 * 3 = 15
5 / 3 = 1
5 % 3 = 2
Consider the a/b scenario in the example above, its result is 1, whereas the actual mathematical result is a decimal value (1.66667). The reason for this is that since we have declared it as an int, the decimal part will be ignored. To handle this, you will need to use type conversion. To read about type conversion, please, click here.
Additionally, you can store the result of a and b in a variable c and then you can print it using output operator (cout). For example:
int c = a + b;
cout << a << " + " << b << " = " << c << endl;
In an expression, multiple operators may be present. Therefore, we must know which operator will be performed first in an expression. Operators are evaluated in the following order:
[] (Parentheses, Array Subscript)* (Multiplication)/ (Division)% (Modulus/Remainder)+ (Addition)- (Subtraction)416 & 6 % 48 → The remainder (%) is calculated first.Just like the C language, C++ also supports the increment (++) and decrement (--) operators. They are two in total and are used in four ways.
++) operator increases the value of its operand by 1.--) operator decreases the value of its operand by 1.Based on their placement (before or after the variable), they are also known as prefix and postfix operators.
++a; // Prefix increment
a++; // Postfix increment
--a; // Prefix decrement
a--; // Postfix decrement
Since these operators operate on a single operand, what is why they are classified as unary operators.
#include <iostream>
using namespace std;
int main()
{
int a = 1;
++a;
cout<< "a=" << a <<endl;
a++;
cout<< "a=" << a <<endl;
--a;
cout<< "a=" << a <<endl;
a--;
cout<< "a=" << a <<endl;
return 0;
}
a=2
a=3
a=2
a=1
The integer variable a is initialized to 1. Operators ++a and a++ increment the values. So, value of a is increased to 2 and then 3. Operators – -a and a- – decrement the value of a. So, value of decreases to 2 and then to 1 again.
Apparently, both increment operators (whether postfix or prefix) perform the same functionality i.e., they are incrementing. Similarly, both decrement operators (whether postfix or prefix) are decrementing. So, what is the difference?
a++ or a--) → First, the value of a is returned, then it is incremented or decremented.++a or --a) → First, the value of a is incremented or decremented, then the updated value is returned.#include <iostream>
using namespace std;
int main()
{
int x = 5, y;
y = x++; // y = 5, x becomes 6
cout << "x = " << x << ", y = " << y << endl;
return 0;
}
x = 6, y = 5
In the code above, first, the original value of x (5) is assigned to y.Then, x is incremented (so x becomes 6).
Now, let’s write another program to explain this in detail.
#include <iostream>
using namespace std;
int main() {
int x = 5, y;
y = ++x; // x becomes 6, y = 6
cout << "x = " << x << ", y = " << y << endl;
return 0;
}
x = 6, y = 6
In the code above, first, the x is incremented (so x becomes 6). Then, the new value of x (which is 6) is assigned to y.
The same happens in case of postfix decrement (a- -) and prefix decrement operator (--a). The only difference will be that instead of incrementing the value we will be decrementing it.
In C++, you can write code in multiple ways. One of these is using arithmetic assignment operators, which help you use operators in a shortcut way. This makes the code easier and reduces typing, which saves time.
For example, instead of writing:
a = a + 4;
You can also write it as:
a += 4;
In this statement, you are adding 4 to the variable a and storing the new value in a. The same operation is being performed in the previous statement as well. Execute the program below and it will give the same output as the previous statement.
#include <iostream>
using namespace std;
int main() {
int result = 5;
int answer = 7;
result += 5; // Equivalent to result = result + 5;
cout << "Result is: " << result << endl;
answer %= 2; // Equivalent to answer = answer % 2;
cout << "Answer is: " << answer;
return 0;
}
Result is: 10
Answer is: 1
Relational operators are used to compare two values. These values must belong to one of the C++ data types.
Additionally, these operators define the relationship between two operands, such as checking whether they are equal or determining their order.
Below is a table listing relational operators:
| Operator | Functionality |
|---|---|
== |
Equal to |
!= |
Not equal to |
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
>= |
Greater than or equal to |
These operators are used in loops or if conditions, which you will learn about in the next tutorial posts.
For example, if a value is equal to another, a specific output should be displayed; otherwise, a different output will be shown. Let’s see how these operators can be used.
#include <iostream>
using namespace std;
int main() {
int num = 5;
cout << "sum is less than 10: " << (num < 10) << endl;
cout << "sum is equal to 10: " << (num == 10) << endl;
cout << "sum is greater than 10: " << (num > 10) << endl;
return 0;
}
sum is less than 10: 1
sum is equal to 10: 0
sum is greater than 10: 0
num with a value of 5.num is equal to 10.num is greater than 10.num is less than 10.num is less than 10, the condition (num < 10) will output 1, while the other conditions will return 0. This means whenever a condition is true, the result is 1; otherwise, it is 0.One capability that sets operators in C++ apart from most other languages is that many of them can be redefined for your own custom types, a feature called operator overloading. By default, an operator like + only knows how to work with built-in types such as int or double; it has no idea how to “add” two objects of a class you have written yourself, such as a Point class representing coordinates. Operator overloading lets you teach C++ what + should mean for that class, so that writing point1 + point2 becomes possible and reads naturally, instead of forcing every user of your class to call a separately named function like point1.add(point2). Not every operator can be overloaded – the scope resolution operator :: and the ternary operator ?: are among the few exceptions – but arithmetic, comparison, and stream operators are commonly overloaded in real C++ code, which is part of why understanding the base behavior of operators in C++ thoroughly, as covered above, is the necessary foundation before extending that behavior to your own types.
The natural next topic to learn is covered in our article on If Else Statement in C++. Readers wanting the formal language specification can consult isocpp.org.
No Comments