Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Operator and Expressions

Operators : Operators are signs/symbols used to perform certain task. (or operation)
OR
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations.

C++ has rich set of built-in operators. The following types of operators are studied in our
course.
• Arithmetic Operators (-,+,*,/,%)
• Relational Operators ( >, >= ,<= ,= =, != )
• Logical Operators ( !, ||, &&)
• Assignment Operator (=)
• Shorthand Operators (Arithmetic Assignment Operator) ( +=, –=, *=, /=, %=)
• Unary Arithmetic Operator (+ / – )
• Increment/Decrement Operators (++ / – –)
• Ternary Operator (Conditional Operator)
• Special Operators

Arithmetic Operators : There are five basic Arithmetic operator. They are +, –, *, /, % The
operation of these is as shown below :
If variable A holds value 15 and variable B holds value 25 , then,

Operator Description Example

+ Adds two operands A + B will give 40

- Subtracts second operand from the first A – B will give -10

* Multiplies both operands A * B will give 375

/ Divides numerator by de-numerator B / A will give 1

% Modulus Operator and remainder of B % A will give 10


after an integer division

Relational Operators : There are six relational operators in C++ language. They are
• Greater than (>)
• Greater than or equal to (>=)
• Less than (<)
• Less than or equal to (<=)
• Equal to (==)
• Not equal to ( != )
Assume variable A holds 10 and variable B holds 20, then –

Operator Description Example

Checks if the value of left operand is


> greater than the value of right operand, (A > B) is not true.
if yes then condition becomes true.

Checks if the value of left operand is


greater than or equal to the value of
>= (A >= B) is not true.
right operand, if yes then condition
becomes true.

Checks if the value of left operand is


< less than the value of right operand, if (A < B) is true.
yes then condition becomes true.

Checks if the value of left operand is


less than or equal to the value of right
<= (A <= B) is true.
operand, if yes then condition becomes
true.

Checks if the values of two operands are


== equal or not, if yes then condition (A == B) is not true.
becomes true.

Checks if the values of two operands are


!= equal or not, if values are not equal then (A != B) is true.
condition becomes true.

Logical Operators : There are three logical operators in C++ language. These operators are
used to check the condition between two values.
They are AND (&&), OR ( || ) , NOT (!)
Assignment Operator (=) :

1. Assignment Operator is used to assign a value to a variable.


2. Assignment Operator is denoted by equal to (=) sign.
3. This operator copies the value at the right side of the operator into the left side variable.
4. Assignment Operator is binary operator.
5. Assignment Operator has lower precedence than all other operators except comma
operator.

Example : z= x + y ; In this z is assigned the value of sum of x and y ;

A= 10 ; valid The value 10 is assigned to A.

25 = b ; invalid as unknown value of b can not be assigned to 25;

Shorthand Operators :
• This operator is also called Arithmetic Assignment Operator)
• This operator is applicable to Arithmetic Operators
• There are five Shorthand Operators
viz : +=, – =, *=, /=, %=
• They are used as shown in below table :
Operator Symbol Name of Operator Example Equivalent Construct

+= Addition assignment a+=5 a=a+5

-= Subtraction assignment a-=5 a=a-5

*= Multiplication assignment a*=5 a=a*5

/= Division assignment a/=5 a=a/5

%= Remainder assignment a%=5 a=a%5

You might also like