Discuss different logical operation in detail.

This answer is restricted. Please login to view the answer of this question.

Login Now

They compare or evaluate logical and relational expressions. Following table shows all the logical operations supported by C language. Assume variable A holds 1 and variable B holds 0 then:

Operators Description Example
&& (Logical AND) If both the operands are non-zero, then the condition becomes true. (A && B) is false.
|| (Logical OR) If any of the two operands is non-zero, then the condition becomes true. (A || B) is true.
| (Logical NOT) It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. !(A && B) is true.

This is the program to demonstrate the use of logical operators

#include <stdio.h>

main() {

   int a = 5;
   int b = 20;
   int c ;

   if ( a && b ) {
      printf("Line 1 - Condition is true\n" );
   }
    
   if ( a || b ) {
      printf("Line 2 - Condition is true\n" );
   }
   
   /* lets change the value of  a and b */
   a = 0;
   b = 10;
    
   if ( a && b ) {
      printf("Line 3 - Condition is true\n" );
   } else {
      printf("Line 3 - Condition is not true\n" );
   }
    
   if ( !(a && b) ) {
      printf("Line 4 - Condition is true\n" );
   }
    
}

The output of above program is

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

 

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .