Interesting fact about logical AND operator

Frenzz Observe this Program

Program

#include<stdio.h>void main() {  int i=1; 
  if((--i)&&(i+10))
   {    
    printf("True block");
   }
  else   
  {    
   printf("False Block");
  }
  printf("\n value of i is %d",i);
 }


Output



Explanation

In above program,initial value of i is 1. In if statement,there are two conditions joined by logical AND.

Logical AND first checks the first condition.If it is true then it'll check second one otherwise it'll skip the second one.

In above program first condition is  --i  so the value is 0 which is false so it skips the second statement. That is the reason why i value is 0.



Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment