Javascript required

C++ Operators

Prev Next


The Operators are represented by Operators and objects of the operations are referred to as operands.
For an instance, 4+5 Here '+' is an operator which works on operands(4 and 5).


Let us discuss these operators in detail.

Arithmetic Operators


To do arithmetic, C++ uses operators. It provides operators for five basic arithmetic calculations : adiition, subtraction, multiplication, division and remainder which are +,-,*,/ and % repectively. Each of these operators is a binary operator i.e., it requires two values(operands) to calculate a final answer. Apart from these binary operators, C++ provides two unary arithmetic operators(that require one operands).

Unary Operators


Operators that act on one operand are referred to as unary operators.
  1. Unary +
    The operator unary '+' precedes an operand.
    if a = 5 then +a means 5.
    if a = 0 then +a means 0.
    if a = -4 then +a means -4.
  2. Unary -
    The operator unary '-' precedes an operand.
    if a = 5 then -a means -5.
    if a = 0 then -a means 0(there is no quantitiy known as -0).
    if a = -4 then -a means 4.

    This operator reverse the sign of the operand's value.


Binary Operators


Operators that act upon two operands are referred to as binary operators.
Following table summarizes the working of binary arithmetic operators :
Symbol Name Example Result Comment
+ addition 6+5
5+6
11
11
adds value of its two operands.
- subtraction 6-5<
5-6
1
-1
subtracts the value of right operand from left operand.
* multiplication 6*5
5*6
30
30
multiplies the value of its two operands.
/ division 60/5 12 divides the value of left operand with the value of right operand.
% modulus & (promounced mo-du-lo) or remainder 60%5
6%5
0
1
divides the two operands and gives the remainder resulting

Increment/Decrement Operators (++,--)


C++ includes two useful operators not generally found in oother computer languages(except C). These are the increment and decrement operators, ++ and --. The C++ name itself is influenced by the increment operator ++. The operator ++ adds 1 to its operand, and -- subtract one. In other words,
a = a + 1 ; is the same as
++a ; or a++ ;
and
a = a - 1 ; is the same as
--a ; or a-- ;

Relational Operators


In the term realtional operator, relational refers to the relationship that values (or operands) can have with one another. The six relational operators are :
< (less than), <= (less than equal to), == (equal to)
> (greater than), >= (greater than equal to ), != (not equal to)

Following Table summarizes the working of relational operators in C++.
Symbol Name Example Result Comment
== comparison (equality) 6 == 5 0 i.e. false returns boolean truth value (0 for false, 1 for true).
< less than 6 < 5
5 > 6
0
1
return boolean truth value.
<= less than equal to 6 <= 5
5 <= 6
0
1
return boolean truth value.
> greater than 60 > 5
5 > 6
1
0
return boolean truth value.
>= greater than equal to 6 >= 5
5 >= 6
1
0
return boolean truth value.
!= Not equal to 6 != 5
5 != 6
0
0
return boolean truth value.
Prev Next