Operators in c++

Operators in C++

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
  • Here, the value on the right (10) is assigned to the variable on the left (x).

Arithmetic operators

In addition to the assignment operator, there are six important integer arithmetic operators in C++, which are listed in the table below:

OperatorsFunctionality
+Addition
Subtraction
*Multiplication
/Division
%Modulus (Remainder)
Negation (Unary minus)

Example:

#include 
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;
}

Output of the Program:

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:

From Highest to Lowest:

  1. ()[] (Parentheses, Array Subscript)
  2. * (Multiplication)
  3. / (Division)
  4. % (Modulus/Remainder)
  5. + (Addition)
  6. - (Subtraction)

Example:

  • 416 & 6 % 48 → The remainder (%) is calculated first.

Unary Operators:

Just like the C language, C++ also supports the increment (++) and decrement (--) operators. They are two in total and are used in four ways.

  • The increment (++) operator increases the value of its operand by 1.
  • The decrement (--) 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.

Example of increment and decrement operations:

#include 
using namespace std;

int main()  
{  
   int a = 1;
   ++a; 
   cout<< "a=" << a <

Output:

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.

Prefix vs. Postfix Difference:

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?

  • Postfix (a++ or a--) → First, the value of a is returned, then it is incremented or decremented.
  • Prefix (++a or --a) → First, the value of a is incremented or decremented, then the updated value is returned.

Example Code of Postfix increment:

#include 
using namespace std;

int main()  
{  
   int x = 5, y;
    y = x++;  // y = 5, x becomes 6
    cout << "x = " << x << ", y = " << y << endl;
      
   return 0;
}  

Output of the Above Program:

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).

Example code of prefix increment:

Now, let’s write another program to explain this in detail.

#include 
using namespace std; 

int main() {
  
   int x = 5, y;
    y = ++x; //  x becomes 6, y = 6
    cout << "x = " << x << ", y = " << y << endl;
 
    return 0; 
}

Output of the above program is:

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.

Arithmetic Assignment Operators:

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 
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; 
}

Output:

Result is: 10
Answer is: 1

Relational Operators:

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:

OperatorFunctionality
==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.

Example of Relational Operators

#include 
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

Understanding the Program:

  • In this program, we have a variable num with a value of 5.
  • The program checks:
    • Whether num is equal to 10.
    • Whether num is greater than 10.
    • Whether num is less than 10.
  • Since 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.

No Comments

Leave a Reply