What is Boolean Expression
In this article, we will see what is Boolean Expression.
Before starting with the topic directly lets us see what is a Boolean expression. It is an expression that always yields two values either true or false when evaluated. If the condition is true then it will return true or false and vice versa.
Let’s take one simple example that will clear the concept of Boolean expression so the expression (5>2) i.e. 5 greater than 2 as we can see it is true that means 5 is greater than 2 therefore the result will be true as we can see the expression yields true as value, therefore, it is called Boolean expression.
Need for Boolean Operators :
When more than two expressions are to be evaluated (expression 1, expression 2) so it is done using boolean operators so we need to connect them using Boolean operators like (expression 1, boolean operator, expression 2).
Boolean Operators :
Boolean operators are used to connecting more than two expressions so that they will be evaluated. When you want multiple expressions to be evaluated then we use Boolean operators and using that multiple expressions are evaluated and we get the answer as true or false.
Types of Boolean Operators :
- AND most of the time, it can be used as AND or it depends on the programming language like in C CPP programming it is represented as &&.
- OR most of the time, it can be used as OE or it also depends on the programming language like in C CPP programming it is represented as ||.
- NOT also is represented as NOT or depends like in C, CPP programming is represented as! (exclamation mark).
Uses of Boolean operators :
- AND operator is used when you want both expressions to be evaluated as true.
- OR operator is used when you want either one of the two expressions should be evaluated as true.
Applications of Boolean expressions :
- Boolean Expressions are used in programming concepts like if else condition, switch cases, etc.
- It is used in Boolean algebra, digital logic, and design.
Examples: let’s check if 5 is greater than 2 and less than 10 using boolean operators and expressions so in programming they will be written like (5>2&&5<10), as we want both expressions to be evaluated as true so we used AND so the result of this boolean expression will be true as 5, is greater than 2 and it is less than 10, therefore, it will produce GFG! as output in the program given below also various examples are included in it.
C++
#include <iostream>
using namespace std;
int main()
{
if (5 > 2&&5<10)
cout << "GFG!" ;
else
cout << "GeeksforGeeks" ;
return 0;
}
|
C
#include <stdio.h>
int main()
{
if (5 > 2||5<1)
printf ( "GFG!" );
else
printf ( "GeeksforGeeks" );
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
if ( 5 > 6 && 5 < 10 )
System.out.println( "GFG!" );
else
System.out.println( "GeeksforGeeks" );
}
}
|
Python3
if ( 5 > 2 and 5 < 10 ):
print ( "GFG!" )
else :
print ( "GeeksforGeeks" )
|
C#
using System;
class GFG{
public static void Main()
{
if (5 > 6&&5<10)
Console.Write( "GFG!" );
else
Console.Write( "GeeksforGeeks" );
}
}
|
Javascript
<script>
if (5 > 6&&5<10)
document.write( "GFG!" );
else
document.write( "GeeksforGeeks" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Boolean expressions are used in many domains like programming and digital logic as it makes the task and logic clear as a result the program efficiency and readability increase.
Last Updated :
25 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...