Operators In C++
Operators In C++
Operators are the symbols that perform particular operations on the variables. The types of operators are described below:-
- Arithmetic operators
It performs arithmetic operations. The following are the arithmetic operators:-
+ (adds two variables)
- (subtract two variables)
* (multiplies two variables)
/ (divide two variables)
% (divides and returns remainder)
Each of these operators is a binary operator i.e. it require two operands (values) to calculate a final answer.
- Relation operators:-
It compares the variables and determines the relation among different variables. It compares the two values and returns the appropriate result. If the comparison is true, it returns 1 and to 0, if it is false. C++ provides six relation operators they are as follows:-
> (grater then)
< (less than)
>= (greater than equal to)
<= (less than equal to)
== (equal to)
!= (not equal to)
- Logical operators:-
C++ provides three logical operators to combine existing operations.
&& (logical AND)
! (logical not)
- Increment / decrement operators:-
They are unary operators because they operate on one operand (variables).
Increment operator (++) adds 1 to its operand while decrement operator (–) subtract 1 from its operands. That is,
X=x+1 is the same as ++x
&
X=x-1 is the same as –x
E.g.
X=10;
Y=++x;
This produce y to 11 and x to 11.
The code,
X=10;
Y=x++;
This sets y to 10 and x to 11.
- Assignment operator:-
It is us to assign a value or result of an expression to a variable. The symbol ‘=’ is used as an assignment operator. C++ offers arithmetic assignment operator, which combine an arithmetic operator and an assignment operator. There are arithmetic assignment operators corresponding to all the arithmetic operations. They are as follows:-
+=, -=, *=, /= and %=
E.g.
A+=b; (is equal to a =a+b)
a-=b; (is equal to a=a-b)
a*=b; (is equal to a=a*b)
a/=b; (is equal to a=a/b)
a%=b; (is equal to a=a%b)
Related posts:
- Predefined Data Types Predefined data types Data can be of many types (e.g....
- Type Conversion In C++ Type Conversion In C++ This process of converting one predefined...
- C++ Cast Explicit type conversion (casts) in c++ or c++ cast It...
- Relation between sets. If , in any condition two or more sets appears...
- Relations. Any subset of a Cartesian product A×B in which the...