Logical Operators

Logical Operators

  • AND,OR operators are used when we want to use two or more Conditions.

Types Of Logical Operators

  • && Logical AND
  • || Logical OR
  • !  Logical NOT

Logical And (&&) Operator 

Logical And Operator Definition

If both the operations are successful, then the condition becomes true.

Logical And Operator Syntax

expr1 && expr2

Logical And Operator Syntax Example

if( (a>10) && (a<20) )
  printf(?A is in-between of 10 and 20?);

Logical And Operator Example Program

/*  Example Program For Logical And operator In C Programming Language
    little drops @ thiyagaraaj.com
    Coded By:THIYAGARAAJ MP             */

// Header Files
#include<stdio.h>
#include<conio.h>

//Main Function
int main()
{
	// Variable Declaration
    int num1 = 30;
    int num2 = 40;

    if(num1>=40 && num2>=40){
        printf("Example of Logical And operator");
    }

	// Wait For Output Screen
	getch();

	//Main Function return Statement
	return 0;
}

Sample Output:

Example of Logical And operator

Logical OR (||) Operator

Logical OR  Operator Definition

If any one of the operations are successful, then the condition becomes true

Logical OR  Operator Syntax

expr1 || expr2

Logical OR  Operator Syntax Example

if( (a>10) ||  (a<20) )
  printf(?A is greater than 10 or less than  20?);

Logical OR  Operator Example Program

/*  Example Program For Logical Or (||) operator In C Programming Language
    little drops @ thiyagaraaj.com
    Coded By:THIYAGARAAJ MP             */

// Header Files
#include<stdio.h>
#include<conio.h>

//Main Function
int main()
{
	// Variable Declaration
    int num1 = 30;
    int num2 = 40;

    if(num1>=40 || num2>=40){
        printf("Example of Logical Or operator");
    }

	// Wait For Output Screen
	getch();

	//Main Function return Statement
	return 0;
}

Sample Output

Example of Logical OR operator

Logical NOT (!) Operator 

Logical NOT Operator Definition

It is used to reverse the condition of result. That is, if a condition is true, Logical NOT operator reverses the result and produces output as false.

Logical NOT Operator Syntax

!expression

Logical NOT Operator Syntax Example

if( !(a>10)
  printf(?A is less than 10 ?);

Logical Not Operator Example Program

/*  Example Program For Logical Not (!) operator In C Programming Language
    little drops @ thiyagaraaj.com
    Coded By:THIYAGARAAJ MP             */

// Header Files
#include<stdio.h>
#include<conio.h>

//Main Function
int main()
{
    int number;

    printf("Please enter an integer value: ");
    scanf("%d", &number);

    if(!(number>40)){
        printf("Input check greater than 40 - TRUE");
    }else{
        printf("Input check greater than 40 - FALSE");
    }

	// Wait For Output Screen
	getch();

	//Main Function return Statement
	return 0;
}

Sample Output

Please enter an integer value: 35
Input check greater than 40 - FALSE