Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

Logical Operators

Logical Operators are generally used to combine two or more conditions and evaluates to True or False.  But Logical operators work with conditions that can return a boolean value (i.e. True or False) and even variables and constant values too.  When used with other than conditions, Logical operators treat all non-zero values as True and ‘0’ as false.

 

Logical Operator

Meaning

Evaluation Procedure

&&

AND

Evaluates to true only if all conditions are true

||

OR

Evaluates to true any one condition is true

!

NOT

Evaluates true if condition is false and vice versa

Logical AND

Logical AND (&&) checks two boolean values and returns either True or False.  The following is the table showing how Logical AND is evaluated.

Condition1/Boolean Value1

Logical AND (&&)

Condition2/Boolean Value2

Result of Logical AND (&&)

True

&&

True

True

False

&&

True

False

True

&&

False

False

False

&&

False

False

We can understand that Logical AND returns True only if both operands of it are True and returns False in remaining cases.

//code showing how logical && works with numbers, variables and conditions

void main()

{

    int x,y;

    x=4&&5; //x will be 1

    y=x&&0; //y will be 0

    if((x==1)&&(y==0))       //True AND True returns True

        printf(“done”);          //Hence printed

}

Logical OR

Logical OR (||) checks two boolean values and returns either True or False.  The following is the table showing how Logical or is evaluated.

Condition1/Boolean Value1

Logical OR (||)

Condition2/Boolean Value2

Result of Logical OR (||)

True

||

True

True

False

||

True

True

True

||

False

True

False

||

False

False

We can understand that Logical OR returns False only if both operands of it are False and returns True in remaining cases.

//code showing how logical || works with numbers, variables and conditions

void main()

{

    int x,y;

    x=4||5; //x will be 1

    y=x||0; //y will be 1

    if((x==1)||(y==0))  //True OR False returns True

        printf(“done”); //Hence printed

}

Logical NOT

Logical NOT (!) is an unary operator that checks boolean value of its operand and returns either True or False.  The following is the table showing how Logical NOT is evaluated.

Logical NOT (!)

Condition/Boolean Value

Result of Logical NOT (!)

!

True

False

!

False

True

We can understand that Logical NOT returns False when the operand is True and returns True when the operand is False.

//code showing how logical ! works with numbers, variables and conditions

void main()

{

    int x,y;

    x=!4; //x will be 0

    y=!x; //y will be 1

    if(!(y==0))  // NOT False returns True

        printf(“done”); //Hence printed

}

You cannot copy content of this page