The Basic Operators in C++

Now we know what are variables and constants, Also we know about getting user input in C++. So now lets see how do we operate on these variables and constants. There are many operators in C++. In this post we will understand them one by one. So lets begin ..

Assignment Operator ( = )

The assignment operator assigns the value on its right hand side to the element on its left hand side. Remember that the assignment operation always takes place from right to left. In simple terms, right side is stored in the left side.

For example:

Arithmetic Operators ( +, -, *, /, % )

Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulo (%)

The first four operators are the common ones. Only the Modulo operator might be new for you guys. The Modulo operator simply returns the remainder.

For example:

Compound Assignment ( +=, -=, *=, /= )

Expression Equivalent Expression
Variable Operator= Value Variable = Variable Operator Value
A += 2 A = A + 2
A -= 2 A = A – 2
A *= 2 A = A * 2
A /= 2 A = A / 2

Increment and Decrement Operator

Increment operator (i plus plus) is similar to i+=1 and i=i+1. While Decrement operator (i minus minus) is similar to (i-=1) and i=i-1.

Increment Decrement
i+=1 i-=1
i=i+1 i=i-1

But there are two types of increment and decrement operators. Namely pre and post. So in total there are 4 sets. Pre Increment(++i), Post Increment, Pre Decrement, Post Decrement.

If you are using these operators as a single statement then there is no problem. For example:

If you are about to ‘display’ or ‘assign’ these operators then pre and post comes into picture. For example :

In the above example, you can see i was equal to 1. Then we are storing ++i inside j. Where ++i means pre increment the value of i. So value of i is first increased by 1. Now i becomes 2. And this value is now stored in j. So final values are i is 2 and j is 2.

In the above example, you can see i was equal to 1. Then we are storing i++ inside j. Where i++ means post increment the value of i. So value of i is first stored inside j. So 1 is stored inside j. Now after storing, value of i is increased by 1. Now i becomes 2. So final values are i is 2 and j is 1.

In the similar way you can co-relate the pre and post decrement operators.

Relational Operators ( ==, !=, >, <, >=, <= )

The relational operators help us to relate the value on the right hand side with the value on the left hand side.

== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Note : (==) operator is not the same as (=) operator. Lets see with the help of an example.

a = 1; // this means that we are storing value 1 inside variable a
a == 1; // this means that we are checking whether a is equal to 1 ?

a==1 will either return true or false value. If the value of a is 1 then a==1 will return TRUE value. If the value of a is not 1 then a==1 will return FALSE value. So it is mostly used in conditional statements such as if (a==1) { do this; } 

  • Similarly, (!=) is opposite of (==) operator. Example, (a!=1) will check whether a is not equal to 1 ? 
  • (>) operator.  For example, (a>1) will check whether a is greater than 1 ?
  • (<) operator. For example, (a<1) will check whether a is less than 1 ?
  • (>=) operator. For example, (a>=1) will check whether a is greater than or equal to 1 ?
  • (<=) operator. For example, (a<=1) will check whether a is less than or equal to 1 ?

You can notice that in all the above examples we are asking a question to check the condition is true or false. So we can conclude that relational operators will always return either a true value or a false value. Also remember (==) is not the same as (=) operator.

Examples with numeric constants :

Lets see more examples with variables and constants, Consider a = 10.

Logical Operators (  !, &&, || )

The operator (!) acts as Logical NOT. It changes true to false and false to true. Lets understand this operator will the help of examples given below.

The operator (&&) acts as Logical AND. Output is true when both inputs are True.

A B A && B
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE

The operator (||) acts as Logical OR. Output is false when both inputs are False.

A B A && B
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE

Lets see some examples :

Difference between normal AND (&) and short circuit AND (&&)

First understand that AND operation returns TRUE value when both conditions are TRUE. Normal AND (&) checks both the conditions even if the first condition turns out to be FALSE. But short-circuit AND (&&) is smart. If it finds the first condition to be FALSE then it does not bother to check the next condition. It rounds up the final answer to be FALSE.

Difference between normal OR (|) and short circuit OR (||)

First understand that OR operation returns FALSE value when both conditions are FALSE. Normal OR (|) checks both the conditions even if the first conditions turns out to be TRUE. But short-circuit OR (||) is smart. If it find the first condition to be TRUE then it does not bother to check the next condition. It rounds up the final answer to be TRUE.

In the next tutorial we will see the conditional operator.

Leave a Reply