Wednesday, August 26, 2020

9.1.5 Logical Operators

  • Logical operators are used with decision making statements. 
  • These operators are used to control two statements together in control statements.
  • For example, you can check 2 conditions instead of one in an if statement. 
  • T he following about logical operators is being given.

Operators

 

Description

&& (AND)

When both conditions are true then the control statement value becomes

true.

|| (OR)

When any one condition is true, the control statement value becomes true.

! (NOT)

These operators are used with the same condition.

When that condition is false then the control statement's value becomes true.



EXAMPLE :-

// Logical operators in C.

#include <stdio.h>
int main()
{
int x = 10, y = 5, z = 10, res;

res = (x == y) && (z > y);
printf("(x == y) && (z > y) is %d \n", res);
res = (x == y) && (z < y);
printf("(x == y) && (z < y) is %d \n", res);
res = (x == y) || (z < y);
printf("(x == y) || (z < y) is %d \n", res);
res = (x != y) || (z < y);
printf("(x != y) || (z < y) is %d \n", res);
res = !(x != y);
printf("!(x == y) is %d \n", res);
res = !(x == y);
printf("!(x == y) is %d \n", res);

return 0;
}

OUTPUT

(x == y) && (z > y) is 0 (x == y) && (z < y) is 0 (x == y) || (z < y) is 0 (x != y) || (z < y) is 1 !(x == y) is 0 !(x == y) is 1


No comments:

Post a Comment

Please do not any spam link in Comment Box