C++ Operators
Operators are used to perform operations on variables and values.They are of many types so get straight to them
Assignment operator (=):
The assignment operator assigns a value to a variable.
a= 5;
This statement assigns the integer value 5 to the variable x. The assignment operation always takes place from right to left, and never the other way around:
see an example
so we assigned value 5 to a and then printed to it
SWAPPING OF VARIABLES :
Now see an example of swapping the variables----------
for swapping two values we need a third variable,
first we assign the value of c equal to a then we make b=c so b becomes = a & declaring a=b so that a becomes b.
So thus we can change the value assigned to an operator.
some combination of operators are shown below-----------------------
“+=”:
This operator is combination
of ‘+’ and ‘=’ operators. This operator first adds the current value of the
variable on left to the value on right and then assigns the result to the
variable on the left.
Example:
(a += b) can be written as (a = a +
b)
If initially value stored in a is 7.
Then (a += 6) = 13.
“-=”:
This operator is combination
of ‘-‘ and ‘=’ operators. This operator first subtracts the value on right from
the current value of the variable on left and then assigns the result to the
variable on the left.
Example:
(a -= b) can be written as (a = a -
b)
If initially value stored in a is 7.
Then (a -= 6) = 1.
“*=”:
This operator is combination
of ‘*’ and ‘=’ operators. This operator first multiplies the current value of
the variable on left to the value on right and then assigns the result to the
variable on the left.
Example:
(a *= b) can be written as (a = a *
b)
If initially value stored in a is 7.
Then (a *= 6) = 42.
“/=”:
This operator is combination
of ‘/’ and ‘=’ operators. This operator first divides the current value of the
variable on left by the value on right and then assigns the result to the variable
on the left.
Example:
(a /= b) can be written as (a = a /
b)
If initially value stored in a is
6. Then (a /= 2) = 3.
Example:
(a += b) can be written as (a = a + b)
If initially value stored in a is 7. Then (a += 6) = 13.
“-=”:
Example:
(a -= b) can be written as (a = a - b)
If initially value stored in a is 7. Then (a -= 6) = 1.
“*=”:
Example:
(a *= b) can be written as (a = a * b)
If initially value stored in a is 7. Then (a *= 6) = 42.
“/=”:
Example:
(a /= b) can be written as (a = a / b)
If initially value stored in a is 6. Then (a /= 2) = 3.
Arithmetic operators ( +, -, *, /, % ) :
They are of two types----------
Operators that
operates or works with a single operand are unary operators. For example: (++ ,
–)
2. Binary Operators:
Operators that
operates or works with two operands are binary operators. For example: (+ , – ,
* , /)
now discussing about Unary Operators we can see there are five types of arithmetical operations .
The five arithmetical operations supported by C++ are:
operator
|
description
|
+
|
addition
|
-
|
subtraction
|
*
|
multiplication
|
/
|
division
|
%
|
modulo
|
Operations of addition, subtraction, multiplication and division hint to their respective mathematical operators. The modulo operator, represented by a percentage sign (%), gives the remainder of a division of two values. For example:
int x;
x = 5 % 3 ;
results in variable x containing the value 2, since dividing 5 by 3 results in 1 , with a remainder of 2.
results in variable x containing the value 2, since dividing 5 by 3 results in 1 , with a remainder of 2.
similarly
int a = 10 ,b = 2 , c= 5 ,d;
cout<<a+b; //will give you 12
cout<<a-b; //will give you 8
cout<<a*b; //will give you 20
cout<<a/b; //will give you 5
d=c/b;
cout<<d; /*will give you 2 as 5/2 is 2.5 but it will return the integer value as you prefixed d as integer*/
No comments:
Post a Comment