This blog is under construction

Sunday, 22 April 2012

Logical operators in C

Logical operators are used to perform logical AND, OR and NOT operations.  It is used to connect one or more relational/conditional expressions to form a complex logical expression.  Any expression that evaluates to 0 is considered to be false and any expression that evaluates to non-zero value is considered to be true.

Logical operator example in C:
int x = 10, y = 20;
if ((x > 10) && (y > 10) {  // checks whether both the expressions are true
  printf("x and y are greater than 10");
}

if ((x > 10) || (y > 10) {  // checks whether atleast any one of the expression is true
  printf("any one or both x & y are greater than 10");
}

if (!(x > 10)) { // checks whether x is not greater than 10
        printf("x is not greater than 10");
}

Logical operators in C:
 Operator  Operation
 &&  logical AND
 ||  logical OR
 !  logical NOT

Below is the truth table for AND, OR and NOT logical operators.

Truth table for logical AND operator:


Operand1  Operand2  Result
 False False False
 False True False
 True False False
 True True True


Truth table for logical OR operator:


 Operand1 Operand2 Result
 False False False
 False True True
 True False True
 True True True

Truth table for logical NOT operator:


 Operand Result
 False True
 True False


Example C program using Logical Operators:
 
  #include <stdio.h>
  int main() {
        int a, b;
        printf("Enter two number: ");
        scanf("%d%d", &a, &b);
        if ((a > 100)  && (b > 100)) {
                printf("Both %d and %d are greater than 100\n", a, b);
                printf("Assigning 50 t0 b\n");
                b = 50;
        }

        if ((b > 100) || (a > 100)) {
                printf("\nHitting Logical OR\n");
                printf("%d is greater than 100\n\n", a);
        }

        if (!(b > 100)) {
                printf("%d is not greater than 100\n", b);
                printf("Hitting Logical NOT\n");
        }
        return 0;
  }


  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter two number: 101 200
  Both 101 and 200 are greater than 100
  Assigning 50 t0 b

  Hitting Logical OR
  101 is greater than 100

  50 is not greater than 100
  Hitting Logical NOT



3 comments:

  1. int x = 10, y = 20;
    if ((x > 10) && (y > 10) { // checks whether both the expressions are true
    printf("x and y are greater than 10");
    sir please explain this ??

    ReplyDelete
  2. Dell Laptop Service center are giving repair service at the door. We should high quality Dell out of warranty Laptop Repair, removal of virus, screen removal, wireless network set up, battery removal, motherboard replacement to several other are offered at budget friendly price and it’s Negotiable. We can fix them all in time by our well experience and certified technicians. If you want to repair your laptop in front of your eyesight, than you may call us: 7217871051

    ReplyDelete
  3. Logical Operators always produces boolean results, either TRUE(non-zero value) or FALSE(zero value). There are 3 logical operators in C language AND(&&), OR(||) and NOT(!)
    Logical operators in C

    ReplyDelete