The Logic in Logical Operators

Shafi Sahal
Geek Culture
Published in
4 min readApr 20, 2021

--

Relational operators are used to evaluate or test the relation between two expressions. There comes a time when we will need to connect two or more such evaluations or expressions and provide a compound output, like when we need to check whether a variable contains a number that is between 10 and 20. Logical operators are used in such cases where we have to combine the evaluation of the expressions.

&&

This is known as the “logical AND” operator. It is used to check whether the condition on both sides of the operator is true.

int a, b;
bool condition1, condition2, result;
a = 10;
b = 20;
condition1 = a == 10; //Contains true as a = 10
condition2 = b == 20; //Contains true as b = 20
result = condition1 && condition2;

Here variable result would contain true as both of the conditions are true. For AND operator, both of the conditions on its sides must be true in order to output true. If any of them were false, the AND operator outputs false. For example, if the value of a was 12, condition1 would be false. So, both sides of the AND operator would not be true and hence it would output false. Here, both of the conditions: condition1 and condition2 combine to form a new condition.

||

This is known as the “Logical OR” operator. It checks whether either of the condition on both sides is true.

int a, b;
bool condition1, condition2, result;
a = 10;
b = 20;
condition1 = a == 10; //Contains true as a = 10
condition2 = b == 20; //Contains true as b = 20
result = condition1 || condition2;

Here result would contain true as both the conditions are true. Even if one of the conditions was false, the result would still contain true. For example, even if the value of a was 12, the result would still contain true as condition2 was true. However, if both of the conditions were false, the result would contain false.

!

This is known as the “Logical NOT” operator. It just changes the current boolean value to its opposite.

int a, b;
bool condition1, condition2, result;
a = 10;
b = 20;
condition1 = a == 10; //Contains true as a = 10
condition2 = b == 20; //Contains true as b = 20
result = !(condition1 && condition2);

Here result would contain false as it was containing true at first but was changed to the opposite boolean value which is false after applying the NOT operator. So, basically, if the NOT operator is applied on a condition that is false, the new value would become true and vice versa.

Checking whether the value is between a range

So, for a better understanding, let’s suppose that we want to check whether the value of a variable is between 10 and 20. So, if the value is 2, which is not between 10 and 20, false should be stored in a variable. If the value is 15, which is between 10 and 20, true should be stored in a variable. We can write a program for that.

Before writing a program think of the steps needed to accomplish the aim and write it as an algorithm.

Algorithm

  1. Declare a variable of type integer, num to store the number.
  2. Declare two variables of type boolean, isNumGreaterThan10 and isNumLessThan20 to store the conditions.
  3. Declare a variable of type boolean, isNumInRange to store the result.
  4. Assign the value of 12 to num.
  5. Assign the value of (num > 10) to isNumGreaterThan10 and the value of (num < 20) to isNumberLessThan20.
  6. Assign the value of (num > 10 && num < 20) to isNumInRange.

Program

#include <stdio.h>
#include<stdbool.h>
int main()
{
int num;
bool isNumGreaterThan10, isNumLessThan20;
bool isNumInRange;

num = 12;
isNumGreaterThan10 = num > 10;
isNumLessThan20 = num < 20;

isNumInRange = isNumGreaterThan10 && isNumLessThan20;

/*
The line of code below is used to display
the output of boolean values
as they don't have format specifiers.
It will be explained in the next article.
Right now no need to understand this line.
*/
printf("%s", isNumInRange ? "true" : "false");
}

Output

true

Try assigning num the value of 22. The program would output false as it is not in the range. Try with different values and ranges. Also, with all of the previous knowledge, try to do some new creative programs of your own.

Previous => Relational Operators

Next => How to interact with users in programming?

--

--

Shafi Sahal
Geek Culture

Developer, adventure lover and a truth seeker. Like to write about topics from a unique perspective. Twitter: https://twitter.com/_shafisahal.