LOGICAL OPERATORS

Logical operators for performing logical operations of the given relational expressions or the variables. The logical operators in C are used for combining multiple constraints/ conditions or for complementing the evaluation of any original condition that is under consideration.

Types of Logical Operators in C

We have three major logical operators in the C language:

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

Functions of Logical Operators in C

Logical NOT (!) Operator

This type of operator returns true whenever the conditions that are under condition are not at all satisfied. In any other case, it is bound to return false.

Syntax:

if(!enrolled)

{

// Enroll the student

}

Logical OR (||) Operator

This type of operator returns true even when both or even one of the conditions that are under consideration are satisfied. In any other case, it is bound to return false.

Syntax:

if(today == Sunday || today == Saturday)

{

// It is a holiday

}

Logical AND (&&) Operator

This type of operator returns true when both the conditions that are under consideration happen to be satisfied. In any other case, it is bound to return false.

Syntax:

if (skill == ‘C’ && experience >= 2)

{

//He is a handsome guy

}

Example:

#include <stdio.h>

main() {

int a = 5;
int b = 20;
int c ;

if ( a && b ) {
printf("Line 1 - Condition is true\n" );
}

if ( a || b ) {
printf("Line 2 - Condition is true\n" );
}

/* lets change the value of a and b */
a = 0;
b = 10;

if ( a && b ) {
printf("Line 3 - Condition is true\n" );
} else {
printf("Line 3 - Condition is not true\n" );
}

if ( !(a && b) ) {
printf("Line 4 - Condition is true\n" );
}

}

OUTPUT:

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store