LOGICAL OPERATORS

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.

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.

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.

#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" );
}

}
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