× About Us How it works Pricing Student Archive Subjects Blog Contact Us

Enrich your knowledge with our informative blogs

Difference between AND, OR Operators

Amongst all operators, AND and OR are two widely used logical operators in any programming language.  

AND, OR operators fall under logical operators set and are known as Logical AND denoted by && and Logical OR denoted by ||. 

Let’s first discuss the Logical OR.  

Logical OR 

Logical OR is a Boolean operator and is denoted by double pipe symbols (||). 

Certain test conditions are input to this operator and the result obtained is also Boolean i.e. either TRUE or FALSE. 

This operator requires a minimum of two operands to process. 

The general conventions followed for all logical operator is that any non zero number is considered as TRUE (1).  

Incase the expression does not has number, it is converted into its corresponding ASCII value and then used for evaluation.  

A Logical OR returns TRUE (1) when atleast one or more operands are TRUE. It will return FALSE (0) only when all the operands are FALSE  or zero. 

Syntax of Logical OR:  

Operand 1 || Operand 2 

Truth Table for Logical OR 

Operand 1  Operand 2  Operand1 || Operand2 
TRUE   TRUE  TRUE 
TRUE  FALSE  TRUE 
FALSE   TRUE  TRUE 
FALSE  FALSE  FALSE 

 

Let’s check a C program to demonstrate the Logical OR that prints a number if it’s either greater than 4 or less than 10 

// C program to demonstrate example of  

// Logical OR (||) operator  

#include <stdio.h> 

int main() 

{ 

    int num = 3; 

    //printing if number less than 10 or greater than 4 

if ( (num < 10) || (num > 4)) 

       printf(“%d”, num); 

else  

     printf(“No Match Found”);  

    return 0; 

} 

Here, check the test condition  

(num < 10) || (num > 4) 

When num=3, the first condition is TRUE but the second condition is FALSE. 

By looking at the TRUTH table, you can easily deduce that ‘if will return TRUE or 1 and the number will be printed on console. 

Logical AND  

Logical AND is denoted by && and just like OR, it works on Boolean operands and values. 

Logical AND returns TRUE or 1 only when all the operands are TRUE or non zero. 

If any one of the operands is zero, it will be FALSE. It is just like multiplication, one zero and the entire thing will be zero or FALSE. 

  Syntax of Logical AND:  

Operand 1 && Operand 2 

Truth Table for Logical AND 

Operand 1  Operand 2  Operand1 && Operand2 
TRUE   TRUE  TRUE 
TRUE  FALSE  FALSE 
FALSE   TRUE  FALSE 
FALSE  FALSE  FALSE 

 

Let’s check a C program to demonstrate the Logical AND that prints a number if it’s greater than 4 and less than 10 

// C program to demonstrate example of  

// Logical OR (||) operator  

#include <stdio.h> 

int main() 

{ 

    int num = 3; 

    //printing if number less than 10 or greater than 4 

if ( (num < 10) && (num > 4)) 

      printf(“%d”, num); 

else  

      printf(“No Match Found”);  

    return 0; 

} 

We are taking the same program used in Logical OR, for better understanding. 

Here the test condition is 

(num < 10) && (num > 4) 

When num is replaced by 3, the first statement is TRUE while the second is FALSE. This makes the entire expression turn FALSE making the control of program to shift to “else” part in the program. 

No Match found” will be printed on console. 

Now, Let’s draw the basic difference between the two operators: 

  OR  AND 
Symbol  || (two pipes)  && (Two Ampersands) 
Working   Returns TRUE when any of the operand is TRUE.  Returns TRUE only when all the operands are TRUE. 
Similarity   Works like arithmetic Addition  Works like arithmetic Multiplication 

 

Read More – Coding and Programing Questions

View More – Useful links for Your Child’s Development 

Unveil your gateway to a lucrative career!
Unveil your gateway to a lucrative career!

Unleash the power of true logic building with Real-time instructions and live coding exposure.

Book A Demo Class

Tel Guru
Tel Guru

Register For The Demo Class

[footer-form]