5.8 – Logical expressions in C language

by subbu on September 19, 2013

Need of logical operators:

So far a single condition is used to check any possibility. Some times we need to check multiple conditions to take a decision.

Say for example, if we want to check whether a student is pass or fail then we must check whether he got pass marks in all the subjects? If so considers as pass, if he fails to get pass mark in any one subject then considers as fail.

Let us see another example, if we want to check whether an integer is a three digit number or not then we must check whether it is in between -999 to -100 or 100 to 999. If so then it is a three digit number otherwise it is not a three digit number.

In both the cases we need to check multiple conditions to take a decision. Logical operators are used to add multiple conditions to take a decision. C language provides the following logical operators to add multiple conditions

Logical operator Meaning
&& AND logical operator
|| OR logical operator
! Not logical operator

&& (AND logical operator):

It is a logical operator used to add multiple relational expressions (conditions) to take a decision. The AND logical expression gives true if all the conditions are true, gives false if any one of the condition is false. It is generally represented with a series electrical circuit.

Truth table of AND logical operator:

AND logical operator in C languageExample 1:

10<20 && 40>80 && 60!=30

1     &&   0   &&    1

           0

Here in this example 10<20 gives true (1), 40>80 gives false (0) and 60!=30 gives true (1). The result of total expression would be false (0) because the AND logical expression returns false if any one or more conditions are false.

Example 2:

40!=20 && 20<30 && 80!=30

  1    &&   1    &&  1

            1

Here in this example 40!=20 gives true (1), 20<30 gives true (1) and 80!=30 gives true (1). The result of total expression would be true (1) because the AND logical expression returns true if all the conditions are true.

|| (OR logical operator):

It is a logical operator used to add multiple relational expressions (conditions) to take a decision. The OR logical expression gives true if any one or more conditions are true, gives false if all the  conditions are false. It is generally represented with a parallel electrical circuit.

Truth table of OR logical operator:

OR logical operator in C language

Example 1:

10>20 || 40>=80 || 60!=30

  0   ||    0   ||   1

            1

Here in this example, 10>20 gives false (0), 49>=80 gives false (0) and 60!=30 gives true(1). The result of total expression would be true (1) because OR logical expression gives true if any one or more conditions are true.

Example 2:

40>80 || 20!=20 || 60<30

  0   ||    0   ||  0

            0

Here in this example, 40>80 gives false (0), 20!=20 gives false (0) and 60<30 gives false (0). The result of total expression would be false (0) because OR logical expression gives false if all the conditions are false.

! (NOT logical operator):

It is a negation operator, gives negation to the given condition. The truth table of NOT logical operator is

A !A
1 0
0 1

Example 1:

!(10>30 && 40!=20)     ! ( 0 && 1)    ! (0)    1

Example 2:

!(20!=20 || 80>20 )      ! ( 0 || 1)  →   !(1)      0

Logical expressions:

Combination of relational expressions and the logical operators is called a logical expression. The result of any logical expression is either true (1) or false (0). The priority and associativity of logical operators comparatively with other operators is as follows

Operators Associativity
( ) Left to right
! Right to left
* / % Left to right
+ - Left to right
< > <=  >= Left to right
==  != Left to right
&& Left to right
|| Left to right
= Right to left

We will see the behavior of logical operators through some examples

#include<stdio.h>
int main()
{
int a;
a=40>20&&30!=80; /* 1 and 1 gives 1 */
printf("%d",a);
return 0;
}

Output:
1

#include<stdio.h>
int main()
{
int x;
x=30!=80||40<80&&50==60;
printf("%d",x);
return 0;
}

Output:
1

Logical operator behaviorAs the priority of && is more than ||, 40<80&&50==60 evaluates first and gives 0. As the second step 30!=80 || 0 evaluates and gives 1.

#include<stdio.h>
int main()
{
if(10>80||!0==1&&0==!1)
    printf("Hello");
else
    printf("World");
return 0;
}

Output:
Hello

Logical operator behaviorHere, ! Operator has higher priority and its associativity is left to right. !0 is 1 and !1 is 0.

Previous post:

Next post: