C Logical Operators Tutorial

“Logic is the beginning of wisdom, not the end” Leonard Nimoy

Before reading this section you should already be familiar with operators in general and comparison-operators.

Sometimes we want to run multiple comparisons at the same time and only if the results of comparisons altogether were true, a set of instructions to run, otherwise no! Or we might want to run multiple comparisons and only if ONE of those comparisons resulted true, then a set of instructions to run.

This is where we can use logical-operators!

In this section we will introduce these operators and try to give an example for each one of them.

Here’s an introductory list of Logical-operators:

Operator Symbol
AND Operator &&
OR Operator ||
Reverse the Result (Not) Operator !

Via this operator we can put together multiple comparisons and check to see whether or not the results of the entire comparisons are true. If all of them resulted true, the end result will be true as well otherwise even if one comparison resulted false the end result is false as well.

Example:

#include <stdio.h>

int
main() {

int x = 432;

if (x >= 100 && x<1000 ){
printf("The number of digits in this number is 3");
}
return 0;
}

Result:

The number of digits in this number is 3

In the example above, we’ve run multiple comparisons in the if statement via && operator.

First, we’ve checked to see if the value assigned to the x variable is larger than 100, then followed by this comparison we set && operator to add another comparison and this time we’ve checked to see if the value of the x variable is lesser than 1000.

Now only if the two comparisons result true, in that case we can say the end result in the if statement is true as well and so the body of this statement will run.

Note: as you can see in the example above, the use of `&&` operator comes between each condition.

OR Operator `||`:

Via this operator we can put together multiple comparisons and check to see whether or not the result of only one comparison is true. If one of them resulted true, the end result will be true as well otherwise if all comparisons resulted false the end result is false as well.

Example:

#include <stdio.h>

int
main() {

int x = 432;

if (x >= 1000 || x<200 ){
printf("The number is larger than 1000 or less than 200");
} else{
printf("The number neither larger than 1000 nor less than 200");
} return 0;
}

Result:

The number neither larger than 1000 nor less than 200

In the example above, we’ve run multiple comparisons in the if statement via || operator.

First, we’ve checked to see if the value assigned to the x variable is larger than 1000, then followed by this comparison we set || operator to add another comparison and this time we’ve checked to see if the value of the x variable is lesser than 200.

Now only if ONE of the two comparisons results true, in that case we can say the end result in the if statement is true as well and so the body of this statement will run.

But as you can see the end result of the comparisons in the if statement is false because the value of x variable is neither larger than 1000 nor less than 200. So the body of else section will run instead.

Note: as you can see in the example above, the use of `||` operator comes between each condition.

Reverse the Result (AKA Not) Operator `!`:

The result of a comparison is either true or false. For example the result of 10 > 5 is true.

But via ! operator we can reverse the result of a comparison. Basically when we use this operator, we are telling the compiler that “whatever the end result was I want the opposite of it”.

Note: we put this operator on the left side of a comparison and normally put that comparison in parentheses as well.

Example:

#include <stdio.h>

int
main() {

int x = 432;

if (!(x>1000) ){
printf("x is not larger than 1000");
}
return 0;
}

Result:

X is not larger than 1000

We know that the value of x variable is not larger than 1000 so the result of x> 1000 should be false. But because we reversed the result via ! operator, the end result will be true and the body of the if statement will run.

Note: You’ll see more use cases of ! in while statement and working with files sections.