LECTURE#03
Operators & their types
SUMMARY
· Operators
o Arithmetic operators
o Relational Operators
o Logical operators
o Increment (++) and decrement (--) operators
o Binary v/s unary operators
Arithmetic operators
In C-Language we have usual arithmetic operators, these all are binary operators it means they need two operands to perform an operation.
Plus | Minus | Multiplication | Division | Modulus |
+ | - | * | / | % |
So you all are familiar with these except last one modulus. Modulus operator operates on two integers, it return remainder after division.
Consider an example, we take input from user and tell the user that the number is either even or odd.
As we know that when we divide any number by 2, if the remainder is 1 then the number is odd, if the remainder is 0 then the number is even.
So
X%Y == 0 then number is even
X%Y == 1 then number is odd
Precedence of operators
The arithmetic operators in an expression are evaluated according to their precedence power; precedence power means which operator will be evaluated first and which will be evaluated after and so on. You can force the evaluation by using parenthesis.
Consider an example 2+3*4 =?
What should be the answer? Guess 20?
No it’s not 20; actual answer is 14 because multiplication has more precedence power than plus so first 3*4 = 12 then 2+12 = 14
But if you want to force to do addition first then you can use parenthesis like this
(2+3)*4
So now 2+3 is evaluated first that 5 and then 5*4 = 20
Precedence is depicted in picture below
Operators | ( ) | *,/ | +,- |
Precedence | 1 | 2 | 3 |
Relational operators
Relational operators actually statements that either could be true of false but not both. In real life we making decisions based on conditions for example students are eligible to take admission in BS (CS) if he/she has a percentage greater than 70 in intermediate exams.
Consider another example; we have result of all students stored in list, obtained and max marks of each subject is mentioned in list, now we have to make two list one that passed in examination and another list that fails in examinations. Moreover if any student gains percentage less than 50 then the student is fail otherwise passed in exam.
Following table depicts the relational operators
Greater than | Less than | Equal | Not equal | Greater than or equal | Less than or equal |
> | < | == | != | >= | <= |
Nested conditional flow
C language provides if-else structure to make conditional flow. Consider an example of grading system according to the prospectus
Marks | Grade |
87 – 100 | A – Grade |
72 – 86 | B – Grade |
60 - 71 | C – Grade |
Below 60 | F - Grade |
Flow chart for grading system
Logical operators
Logical operators are very helpful when making decision based on more than one conditions or simple decision making on complex conditions. For example my friend ask to my mother yesterday
Q#: Can I go outside to play cricket?
ANS: Yes you can, if your room is clean and you have completed your home work.
See the flow chart
Logical Operators | ||
And | OR | NOT |
&& | || | ! |
Consider an example for logical OR operator.
Suppose you are going to Cinema Theater for watching film, when you reached at the gate, a gate keeper said to you
“You can only go inside Cinema Theater if you have PASS or you have purchased a ticket”
Consider another example for logical AND operator
Suppose you are going to bank for withdrawal of some cash, but you can only get money from bank
“Either you have check OR you have ATM card with you”
Consider more real life complex example of AND operator, in a news paper a NOKIA announces job vacancy for Shikarpur but they will only call the peoples for interview who has Masters Degree and 4 years experience and living in Shikarpur district so how we will design flow chart for this scenario
Suppose we have list of applications available, now we have to filter the list and make two new lists, one that are suitable candidate for interview and second for not eligible
Increment (++) & decrement (--) operators
These both are the unary operators it means they work with single operand or we can say with single variable like
int x = 10;
x++;
x++ means x = x+1
it means x is increased by 1
similarly decrement operator (--) decreased the value of the variable by 1 like
int x = 10;
x--;
so the value of x becomes 9
Binary v/s Unary operators
Binary operator works with two operand at least like +, - , * etc and unary operators work with only single operand (value) or single variable only like x++;
So increment (--) and decrement (++) operators are best examples of unary operators in c language
No comments:
Post a Comment