Logical Operators in Programming
If you are new to any programming language than the concept of logical operators in programming might be a little confusing to you. The logical operators are now a fundamental part of programming languages. They are used to control the flow of the program, they help your program to make decisions. We will be looking at the detailed explanation of logical operators in programming and how they are used. First, we will look at logic gates to better understand the concept
Logic gates or logical gates are the building blocks of digital computers and digital circuits. They are used to perform logical operations in a circuit. Well, how can we perform logical operations in a circuit? We use Logic gates. Logic gates are electrical circuits that determine whether the current will flow from them or not. The decision is based on the input of the current.
Types of Logic gates
Although there are 7 logical gates, the main 3 gates are enough to understand the rest of them. These are known as AND, OR, and NOT gate. The functions of these gates are simple. The AND gate passes current when both of the inputs are passing current. This means if one of the inputs do not get current, the AND gate will not let any electricity to pass through. The OR gate also has two inputs and one output but there is a little difference. When just one input of the OR gate experiences current, the OR gate will let the current to pass through.
NOT gate, also known as an inverter, is pretty straight forward though, it only has one input and one output. If the current passes from the input of the NOT gate then there will be no current in the output and vice versa.
So you can see where the concept of logical operators in programming comes from. This form of calculation where there are only two states to work upon is known as Boolean Algebra.
After reading about the logic gates you might be able to grasp the concept of logical operators in programming. Logical operators are more commonly knows as boolean operators.
Just like logic gates, there are 3 Boolean operators. AND, OR, and NOT. They work in the same way as logic gates but the only difference is that they are more general. Boolean operators work on boolean expressions and evaluate to TRUE, or FALSE. Let us look at the circuit examples to understand Boolean operators.
In the AND gate, the current passes only when both of the inputs are getting the current. To represent this in Boolean operators we can assume that the state when the current is passing will be “1” or “True”. When the current is not passing the state will be “0 (zero)” or “False”. We can say that if the state of inputs of an AND gate is: TRUE AND TRUE then the current will pass. If one of the inputs is FALSE then the state will be FALSE.
- TRUE (current passing) AND TRUE (current passing) = TRUE (current passing from the output)
- FALSE (current not passing) AND TRUE (current passing) = FALSE (current not passing from the output)
- TRUE (current passing) AND FALSE (current not passing) = FALSE (current not passing from the output)
- FALSE (current not passing) AND FALSE (current not passing) = FALSE (current not passing from the output)
Similarly, the OR boolean operator will evaluate to TRUE if one the inputs are TRUE
- TRUE OR TRUE = TRUE
- FALSE OR TRUE = TRUE
- TRUE OR FALSE = TRUE
- FALSE OR FALSE = FALSE
The NOT boolean operator simply inverts the state
- TRUE NOT = FALSE
- FALSE NOT = TRUE
Let us use a real-life example to understand this a bit more. Imagine that you have to go to your friend’s house. You go to ask your mother. She says “You cannot go until you do your homework AND brush your teeth”. You are only allowed to go when you have done both of the tasks.
Now imagine you have to go to the grocery store. Your mother says “Return to home when you get 6 eggs OR buy a carton of milk”. Here there only one task that needs to be done for you to come back home.
I hope these examples have cleared your mind regarding the boolean operators. These examples can be called boolean expressions. Boolean expressions are a logical statement that evaluates to TRUE or FALSE.
We use these boolean expressions and boolean logic more than we can think. Every statement that involves a logical outcome can be a part of boolean expressions. Every outcome which can either be TRUE or FALSE, exist or does not exist, full or half, any outcome which can have two states is a part of boolean expressions. Now, let us learn about logical operators in programming.
Logical operators in programming
In programming, the logical operators also known as boolean operators are a symbol or words that are used to connect two or more Boolean expressions. The value that is produced is a boolean value, whether “True” or “False”. Logical operators in programming are usually used to control the flow of the program. You will find them as a part of if-else, while loops or other control statements.
These control statements depend on the boolean value in order to function. Let us look at a simple Python if statement.
a = 1
b = 1
if a == b:
print("A is equal to B")
print("A and B are not equal")
In this example, a and b are equal so, a == b will evaluate to TRUE hence the code inside the if block will run. If A and B were not equal the code inside else block would have run. The relational operator like “==” evaluates to a boolean value, either TRUE or FALSE. In our example, the boolean value will be TRUE hence the code in if block will run.
Now let us see how we could use boolean operators in programming to perform much complex decision making.
Using AND boolean operator
a = 1
b = 1
c = 5
d = 3
if a == b and c > d:
print("A is equal to B AND C is greater than D")
print("Either A is not equal to B or C is not greater than D")
In this example, we are using the AND operator to make a decision. the code in if block will only run when A is equal to B AND C is greater than D. As I said earlier that the relational operators evaluate to a boolean value. In this example, the “a == b” part will evaluate to TRUE and the “c > d” part will also evaluate to true. This will create a boolean expression. The AND operator will work on these operands, figure out what to evaluate by looking at both of the operands. The AND operator is one of the most used logical operators in programming.
Using OR boolean operator
a = 10
b = 10
c = 3
d = 1
if a == b or c < d:
print("A is equal to B, or C is less than D, or both statements are true")
print("both of the relational expressions evaluates to false")
The code in the if block will run because one of the relational expression is evaluating to TRUE. The OR operator evaluates to TRUE if one of the relational expression is TRUE. Here A is equal to B but C is not less than D but the if block will run. These type of boolean expressions are easy to understand but in the end, we will look at one complex example
Using NOT boolean operator
a = 1
b = 2
if not a == b:
print("A is not equal to B")
print("A is equal to B")
This might seem a little complex at first but this is very straight forward. In this example of boolean expressions, NOT operator is simply inverting the boolean value. The “a == b” part will evaluate to FALSE because A is not equal to B but the NOT operator will invert that FALSE boolean value to TRUE hence the code inside the if block will run.
A complex example
I will define a more complex example of boolean expressions here. Try to understand the logic and think about what block of code will run.
age = 3
class = 4
height = 71
if (age > 15 and class > 8) or height > 70:
print("You can go in this ride")
print("You are not allowed to go")
Try to think of the results.
Here according to the BODMAS, the boolean expressions inside the brackets will evaluate first. As the age is not greater than 15 and class is also less than 8, the expression will evaluate to FALSE. It will look like this.
if FALSE or height > 70
As the height is greater than 70 the boolean expression will evaluate to TRUE. The OR logical operator returns TRUE if one of the operands is TRUE hence the If block will run.
The knowledge of logical operators/boolean operators is one of the fundamentals. The logical operators in programming are really important nowadays. They are used everywhere. In your automated cars, databases and so much more.
I hope this guide helped you. Comment below if you have any queries. If you have read this article you can also read.