# Operators

## Operators:

Operators are the symbols that are used to perform certain operations on data. C++ provides a variety of operators. Different types of operators in C++ are as follows:

• Relational operators
• Increment and decrement operators
• Compound assignment operators
• Assignment operators
• Logical operators
• Arithmetic operators

### Arithmetic operator

Arithmetic operator is a symbol that performs mathematical operation on data. C++ language provides many arithmetic operators. Following is a list of all operators used in C++:

 Operation Symbol Description Addition + Adds two values Subtraction – Subtracts one value from other Multiplication * Multiplies two values Division / Divides one values by other Modulus % Gives the remainder of division of two integers

### Relational operator

The relational operators are used to specify conditions in programs. A relational operator compares two values. It produces result as true or false. The relational operators are sometimes called comparison operators as they test that are either true or false.

C++ provides the following six basic relational operators:

 Operator Description > Greater than operator returns false if the value on right side of > is less than the value on the left side. Otherwise returns true. < Less than operator returns true if the value on left side of < is less than the value on right side. Otherwise returns false. == Equal to operator returns true if the values on both side of == are equal. Otherwise returns false. >= Greater than or equal to returns false if value on the right side of >= is less than the value on the left side. Otherwise returns true. <= Less than or equal to returns true if value on the left side of <= is less than the value on the right side. Otherwise returns false. != The not equal to operator returns true if the value on the left side of != is not equal to the value on the right side. Otherwise returns false.

### Logical operator

The logical operators are used to evaluate compound conditions. There are three logical operators in C++ language:

1. AND operator (&&)
2. OR operator (||)
3. NOT operator (!)

AND operator

The symbol for AND operator is (&&). It is used to evaluate two conditions. It produces true result if both conditions are true. It produces false result if any condition is false.

 Condition 1 Operator Condition 2 Result False && False False False && True False True && False False True && True True

OR operator

The symbol used for OR operator is ( | | ). It is used to evaluate two conditions. It gives true result if either condition is true. It gives false if either condition is false.

 Condition 1 Operator Condition 2 Result False && False False False && True True True && False True True && True True

Not operator ( ! )

The symbol for not operator is ( ! ). It is used to reverse the result of a condition.

 Operator Condition Result ! True False ! False True

### Assignment operator:

The assignment (=) is used in assignment statement to assign a value or computational result to a variable.

The name of the variable is written on the left side of the operator and the value is written on the right side of the operator. A statement that assigns a value to a variable is called assignment statement.

SYNTAX

Variable = expression;

EXAMPLES

A = 100;

C = A + B;

### Increment and Decrement operator

INCREMENT OPERATOR

The increment operator is used to increment the value by 1. It is denoted by the symbol ++. It is a unary operator and works with single variable. Increment operator can be used in two forms.

• Prefix form:

The increment operator is written before the variable as ++y.

• Postfix form:

The increment operator is written after the variable as y++.

DECREMENT OPERATOR

The decrement operator is used to decrease the value by 1. It is denoted by the symbol –. It is a unary operator and works with single variable. Decrement operator can be used in two forms.

• Prefix form:

The decrement operator is written before the variable as –y.

• Postfix form:

The decrement operator is written after the variable as y–.

### Compound assignment operator

C++ language provides compound assignment operators that combine assignment operator with arithmetic operators. Compound assignment operators are used to perform mathematical operations more easily.

SYNTAX

Variable op = expression;

EXAMPLE

N += 10 which is equivalent to N = N + 10;