May 09, 2018

Logical Operators in C


   In a programming language, an expression that combines two or more expressions is termed as a logical expression.

For combining these logical expressions we use logical operators. If the result of logical operator is true then 1 is returned otherwise 0 is returned. The operands may be constants, variables or expressions.



There is three type of logical operators:


Operator Meaning
&& AND
| | OR
! NOT


Logical Operators
Logical Operators


&& and | | are binary operators while ! is a unary operator.


In C any non-zero value is regarded as true and zero is regarded as false.


AND(&&) operator

The logical AND operator (&&) returns the boolean value true if both operands are true and returns false otherwise.

Syntax:

operand1   &&  operand2  

 The truth table of logical AND operator is:

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


Let's take an example:

int a = 10, b = 9, c = 5, k = 0;

Suppose we have following logical expression:

(a > b) && (b > c)

In the above expression a > b is true and b > c is also true. Therefore the whole expression evaluates to true. Since the logical operators return 1 for true, the value of this expression is 1

Consider some more examples:

Expression Intermediate 
Expression
Result
(a>2) && (b==10) true && false false
(b>=c) && (b>a) true && false false
(c==5) && (c<b) true && true true
a && b true && true true
a && k true && false false


In the last two expressions we have taken only variables. Since nonzero values are regarded as true and zero value is regarded as false, variables a and b are considered true and variable k is considered false.

The following program demonstrates AND operator.

#include <stdio.h>

int main(void)
{
    int a = 5, b = 4, result;

    printf("Value of a = %d\n", a);

    printf("Value of b = %d\n", b);

   
/* result of the logical expression is stored in result */
    result = (a>b) && (b==2);

    printf("Result of (a>b)&&(b==2) : %d\n", result);

   
    return 0;
}


Output:

Value of a = 5
Value of b = 4
Result of (a>b)&&(b==2) : 0



If the sub-expression on the left side of an && operator is false, the expression on the right side will not be checked. Since the entire expression is false if only one of the sub-expressions is false, it would waste CPU time to check the remaining expression.



OR(| |) operator

The logical OR operator (| |) returns the boolean value true if either or both operands is true and returns false otherwise.

Syntax:

operand1   ||  operand2  

 The truth table of logical OR operator is:

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


Let's take an example:

int a = 10, b = 9, c = 5;

Suppose we have following logical expression:

(a > b) || (b > c)

In the above expression a > b is true and b > c is also true. Therefore the whole expression evaluates to true and the value of the whole logical expression is 1.

Consider some more examples:

Expression Intermediate
Expression
Result
(a>2) | | (b==9) true | | true true
(b>=c) | | (b>a) true | | false true
(c==1) | | (c<b) false | | true true
a | | b true | | true true
(c==1) | | 0 false | | false false


The following program demonstrates OR operator.

#include <stdio.h>

int main(void)
{
    int a = 5, b = 4, result;

    printf("Value of a = %d\n", a);

    printf("Value of b = %d\n", b);

   
/* result of the logical expression is stored in result */
    result = (a>b) || (b==2);

    printf("Result of (a>b)||(b==2) : %d\n", result);

   
    return 0;
}


Output:

Value of a = 5
Value of b = 4
Result of (a>b)||(b==2) : 1


NOT( ! ) operator

The logical NOT operator(!) negates the value of the condition. If the value of the condition is false then it gives the result true. If the value of the condition is true then it gives the result false.
 
Syntax:

!operand  

 The truth table of logical NOT operator is:


Condition Result
False True
True False


Let's take an example:

int a = 10, b = 9;

Suppose we have following logical expression:

!(a>3)

In the above expression a > 3 is true. NOT operator negates the value of the condition, thus the result is false.

Consider some more examples:

Expression Intermediate
Expression
Result
! (a>2) ! true false
! ((b>2) && (b>a)) ! false true
! a ! true false
! (a>b) ! true false
! (a||b) ! true false


The following program demonstrates NOT operator.

#include <stdio.h>

int main(void)
{
    int a = 5, b = 4, result;

    printf("Value of a = %d\n", a);

    printf("Value of b = %d\n", b);

   
/* result of the logical expression is stored in result */
    result = (a<b);

    printf("Is a < b : %d\n", result);
    printf("After applying NOT(!) operator\n");
    printf("Is a < b : %d\n", !result);


   
    return 0;
}


Output:

Value of a = 5
Value of b = 4
Is a < b : 0
After applying NOT(!) operator
Is a < b : 1




next    Conditional Operator in C
top    Index
prev    Relational Operators in C





1 comment:

  1. Very informative. Thank you and I wish you success!

    ReplyDelete