Monday, 21 December 2015

OPERATORS USED IN C++


OPERATORS USED IN C++

What is operator:
The operator is a symbol that is used to perform an operations on the operands
The types of operators :
·       Logical
·       Algebraic
·       Relational operators
·       Increment decrement operators
·       Assignment operator
Algebric operators:
“Algebraic operators are used to perform algebraic operations on the operands”

examples
The addition operator returns the sum of its two operands.
 total = 4 + 8; // total is assigned the value 12
The subtraction operator returns the value of its right operand subtracted from its left operand. candyBars = 8 - 3; // candyBars is assigned the value 5
The multiplication operator returns the product of its two operands.
 points = 3 * 7 // points is assigned the value 21
The division operator works differently depending on whether its operands are integer or floating-point numbers. When both numbers are integers, the division operator performs integer division. This means that the result is always an integer. If there is any remainder, it is discarded.
 fullBoxes = 26 / 8; // fullBoxes is assigned the value 3
RELATIONAL OPERATORS:
·       Relational operators are used to compare 2 values
·       There are 6 relational operators
·       They either return true or false


LOGICAL OPERATORS:
·       They are used to perform operations on relational expressions
·       There are 3 basic logical operators in c++
o   AND
o   OR
o   NOT
·       They always evaluate true or false
Logical operators and their meanings:


Examples:



Assignment operators:
IN c++ “=” is the assignment operator it ise used to assign a value or a computational result to a variable
FORMAT:
Variable name= value
·        You cannot write value at left and variable at right it causes an error
INCREMENT DECREMENT OPERATOR:
INCREMENT OPERAOTOR:
·        ++ is the increment operator
·        It is used to increase the value of a variable by 1
·        The constants cannot be used with increment operators ie constants cannot be incremented or decremented
There are 2 types of increment decrement operators:
o   Prefix increment operator
o   Post fix incement oprerator
Prefix increment operator:
                                    In pre fix increment operator the value is incremented before the Execution of the statement
Format: ++variable
Post fix increment
                                    In post fix increment operator the value is incremented before the Execution of the statement
Format:  variable++
Prefix decrement operator:
                                    In pre fix decrement operator the value is decremented before the Execution of the statement
Format:
--variable
Post fix decrement
                                    In post fix decrement operator the value is decremented before the Execution of the statement
Format:
 variable--           


                                    

No comments:

Post a Comment