Operators in C - Part 2

Continuing from Part 1

iv) Logical Operators : This operator is typically used for operating on bits. Or we can say that, it operates on other relational expressions.

Assume I have a scenario where I need to find the greatest of three numbers. What I will do first is, I will compare first two numbers and find out which is bigger. Then I take this bigger number and compare it with the third element. So in order to find a a greatest number, I need to make two comparisions, and in "both" the comparisions the number should be greater. Here comes the usefullness of && operator (Logical And)

Here is how we use it : (iNum1 > iNum2) && (iNum2 > iNum3) : If both the condition evaluates to true, then this expression evaluates to true. If any one fails, then it evaluates to false. Logical And follows the following truth table


Here the A and B specifies the two relational expressions. and the Output is the result of Logical end between the two relational expressions.

Now consider another scenario where I need to find whether I have scored above 90 in atleast one subject. What I will do, I will first compare my first subject mark with 90, if false then I will continue the same with other subjects until I get a subject that has over 90 marks. Now I will use the || operator (Logical Or).

Here is an example : (Mark1 >> 90) || (Mark2 >> 90) || (Mark3 >> 90). If "atleast" one condition is true, then the expression evaluates to true. The Logical Or follows the following truth table.


Now the last Logical operator is the Not Operator (!). It is used for negating an value. It is a unary operator, meaning it operates on only one operand unlike other two. Logical Not follows the following truth table.


v) Increments and Decrement Operators : I have a scenario where I need to perform some set of operations N number of times. So what I will do, I will first assign a variable(say count) to 0. Then for each time the operation is performed, I will add one to it using the expression count=count+1 and then I will compare the value with N. Now people saw that addition/subtaction of one to a variable is more commonly used, and hence they have introduced a very handy operation called Increment and decrement operators.
These are unary operators as they are operating on single operand.
   int iNum=0;

// Increment Operator

// Outputs 1
printf("%d", iNum);

// Decrement Operator

// Outputs 0
printf("%d", iNum);

Ok great, thats a nice little operator. But I need do the comparision, and I need to increment the variable. That is two statements. Why can't I do it in one single statement? C allows us to do something like (iNum++ > 5). Now consider the following example...
   int iNum = 5;   
printf("%d", (iNum++ > 5));

Guessed the output ? No "1" (non zero) is a wrong answer. It will actually output 0 (false). What is the reason ? Because what we say as iNum++ is a post increment operator. The variable will be first utilised in the comparision and then the value of the variable will be incremented. Ok If there is something post, then there should be something called preincrement(++iNum). So if I print (++iNum > 5) in the above scenario it will display 1(non-zero). Similarly there is post decrement(iNum--) and pre decrement(--iNum).

If these increment and decrement operators comes individually in a statement, then post and pre-decrement doesn't make any difference. If it comes alone with some other expressions or in some functions as in the printf statement, it does makes a difference. I will make another detailed post regarding this increment and decrement operator to explain it better.

Lets have a break and continue this in Part 3


Post a Comment

Template designed using TrixTG