Operator in cplusplus

Operator in C++



What is Operator?

Operators are the symbols that are used to perform certain operations on data. Different types of operator are as follow:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Increment & Decrement Operators





Arithmetic Operator

Arithmetic operator is a symbol that performs mathematical operation on data. The arithmetic operators is addition, subtraction, multiplication, division and modulus.

Example:

Suppose we have two variables A and B where A=10 and B=5. Apply arithmetic operations on these variables as follows:


Relational Operators

The operator that are used to specify a relation between two expressions or values are known as relational operators. These operators are <, >, <=, >=, ==, !=.

Example:

If x=10, y=20 and z=5 then find out the output of the following relational expression.


Logical Operator

The logical operators are used to combine relational expressions or relational conditions. The expression containing logical operators is called logical expression or logical conditions. It is also called compound condition or compound expression. 

The output of a logical expression is also in logical form. Its value is either true or false. In C++, following logical operators are used.

  • &&    AND Operator
  • ||      OR Operator
  • !       NOT Operator


The && ( AND ) Operator

The && operator is used to combine two or more relational expression. If all relational expressions are true then the output returned by the compound expression is true. If any one of relational expression in the compound expression is false. the output is false.


The || ( OR ) operator

The OR operator is used to combine more than one relational expressions. If any one of the given relational expression is true, the output will be true otherwise the output will be false.


The ! ( NOT ) Operator

The NOT operator is also known as the unary operator. It inverts the value returned by the relational expression or the compound expression.


Assignment Operator

In C++, assignment operators are used to assign values to variables. For example,

// assign 1 to a
a = 1;1;

Here, we have assigned a value of 1 to the variable a.






Increment Operator


The increment operator is used to increase the value of a variable by 1. It is donated by the symbol ++. It is unary operator and works with single variable.

Increment operator can be used in two forms:

  • Prefix 
  • Postfix


Prefix

In prefix, the increment operator is written before the variable as follows: 

++y;


Postfix

In postfix, the increment operator is written after the variable as follows:

y++;


Decrement Operator

The decrement operator is used to decrement the value of a variable by 1. It is donated by the symbol  --. It is unary operator and works with single variable.

Decrement operator can be used in two forms:

  • Prefix 
  • Postfix


Prefix

In prefix, the decrement operator is written before the variable as follows: 

--y;


Postfix

In postfix, the decrement operator is written after the variable as follows:

y--;

Post a Comment

Previous Post Next Post