# C++operators and their types.

**Q1. Discuss all operators and their types (with examples) that you have learnt so far.**

**Ans. **

**Unary Operators**

Operators that act on one operand are referred to as Unary Operators.

Following are unary operators:

**Unary +**

The operator unary ‘+’ precedes an operand. The operand (the value on which the operator operates) of the unary + operator must have arithmetic or pointer type and the result is the value of the argument.

For example:

If a=5 then +a means 5.

**Unary –**

The operator unary – precedes an operand. the operand of the unary – operator must have arithmetic type and the result is the negation of its operand’s value.

For example,

If a=5 then –a means -5.

** **

**Increment ++ and Decrement —**

The operator ++ adds 1 to its operand, and – subtracts 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–;

However, both the increment and decrement operators come in two varieties: they may either precede or follow the operand. The **prefix** version comes before the operand (as in ++a or –a) and the **postfix** version comes after the operand (as in a++ or a–). The two versions have the same effect upon the operand, but they differ when they take place in an expression.

**Working with prefix version**

When an increment or decrement operator precedes in operand (that is, in its prefix form), C++ performs the increment or decrement operation before using the value of the operand.

For example, the expression

sum = sum + (++count);

will take place in the following fashion. (Assuming the initial values of sum and count are 0 and 10 respectively).

sum sum count

- 0 10 Initial values
- 0 11 First increment it

11 = 0 + 11 Now use it

The prefix increment or decrement operators follow **change-then-use** rule, that is, they first change (increment or decrement) the value of their operand, then use the new value in evaluating the expression.

**Working with postfix version**

When an increment or decrement operator follows its operand (that is, in its postfix form), C++ first uses the value of the operand in evaluating the expression before incrementing or decrementing the operand’s value.

For example, the expression

sum = sum + count++;

will take place in the following fashion. (Assuming the initial values of sum and count are 0 and 10 respectively).

sum sum count

- 0 10 Initial values;

10 0 10 First use it

10 = 0 + 11 Now increment it

The postfix increment or decrement operators follow **use-then-change** rule, that is, they first use the value of their operand in evaluating the expression and then change (increment or decrement) the operand’s value.

** **

**Logical Not !**

The logical NOT operator (!) negates or reverse is false, and vice versa. Or we can say, if the expression is non-zero then !expression is zero (0) and if the expression is zero (0), then ! expression is 1.

For example:

!(5) results into 0 (false) because 5 is non zero (that is, true).

!(0) results into 1 (true) because 0 is zero (that is, false).

** **

**Binary Operators**

Operators that act upon two operands are referred to as Binary Operators.

Following are binary operators:

**Addition +**

The arithmetic binary operator + adds values of its operands and the result is the sum of the values of its two operands.

For example,

4+20 results in 24.

**Subtraction –**

The – operator subtracts the second operand from the first.

For example,

14-3 evaluates to 11.

** **

**Multiplication ***

The * operator multiplies the values of its operands.

For example,

3 * 4 evaluates to 12.

** **

**Division /**

The / operator divides its first operand by the second.

For example,

100 / 5 evaluates to 20.

** **

**Modulus %**

The % operator finds the modulus of its operand relative to the second. That is, it produces the remainder of dividing the first by the second operand.

While using modulus operator both operands must be integer types.

For example,

19 % 6 evaluates to 1, since 6 goes into 19 three times with a remainder 1.

** **

**Relational Operators**

The relational operators determine the relation among different operands. C++ provides six relational operators for comparing numbers and characters. If the comparison is true, the relational expression results into value 1 and to 0, if the comparison is false.

The six relational operators are:

**Less than <**

**Less than or equal to <=**

**Equal to ==**

**Greater than >**

**Greater than or equal to >=**

**Not equal to !=**

** **

**Logical OR ||**

The logical OR operator (||) combines two expressions which makes its operands. The logical OD (“||”) operator evaluates to true that is, 1 if either of its operands evaluate to true.

Following are some examples of logical OR operation:

(4==4)||(5==8) results into 1 (true) because first expression is true.

0||0 results into 0 (false) because neither expression is true (both are 0 that is, false).

**Logical AND &&**

The logical AND operators, written as &&, also combines two expressions into one. The resulting expression has the value 1 (true) only if both of the original expressions (its operands) are true.

Following are some examples of AND operator (&&).

(6==3)&&(4==4) results into 0 (false) because first expression is false.

(4==4)&&(8==8) results into 1 (true) because both expressions are true.

**3. Ternary Operators**

Operators that act upon three operands are referred to as Ternary Operators.

Following are ternary operators:

Conditional operator (?:)

The general form of conditional operator?: is as follows:

expression1 ? expression2 : expressions3;

If expression1 evaluates to true that is, 1 then the value of the whole expression is the value of expression2, otherwise, the value of the whole expression is the value of expression3.

For instance,

result = marks >= 50?’P’:’F’;

The identifier result will have value ’P’ if the test expression marks >= 50 evaluates to true (1) otherwise result will have value ‘F’.