In programming languages, we have various data types to store different types of data. Some of the most used data types are integer, string, float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or False. These values are not case-sensitive depending upon programming languages. The name Boolean comes from the branch of mathematics called Boolean algebra, named after George Bool the mathematician.
What is Boolean Data Type?
The boolean data type is used to store logic values i.e. truth values which are true or false. It takes only 1 byte of space to store logic values. Here, true means 1, and false means 0. In the boolean data type any value other than '0' is considered as 'true'. Boolean values are most commonly used in data structures to decide the flow of control of a program and decision statements.In programming languages, we have various data types to store different types of data. Some of the most used data types are integer, string, float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or False. These values are not case-sensitive depending upon programming languages. The name Boolean comes from the branch of mathematics called Boolean algebra, named after George Bool the mathematician.
Example of Declaration of Boolean Data Type
Below are code examples of how to declare a boolean data type in different languages such as C, C++, Java, Python, and JavaScript.
C++
#include <iostream>
using namespace std;
int main() {
bool a = true;
bool b = false;
bool c = 'yes';
cout<<"a: "<<a<<endl;
cout<<"b: "<<b<<endl;
cout<<"c: "<<c<<endl;
return 0;
}
C
#include <stdio.h>
#include <stdbool.h>
int main() {
bool a = true;
bool b = false;
bool c = 5;
printf("a: %d\n", a);
printf("c: %d\n", b);
printf("c: %d", c);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
boolean a = true;
boolean b = false;
//boolean c = 1; this will give error
//Because in Java only true and false
//can be used in boolean
System.out.println("a: "+a);
System.out.println("b: "+b);
}
}
Python3
a = True
b = False
print("a: ",a)
print("b: ",b)
JavaScript
let a = true;
let b = false;
console.log(a); // print true
console.log(b); // print false
Difference Between Boolean and Other Data Types
In programming languages, there are three types of data which are Booleans, Text, and Numbers. It is important to understand the differences between them and some basics about them.
- Booleans: They are either true (1) or false (0) and take only 1 byte of space in memory. while other data types take 2 to 8 bytes depending on the machine.
- Numbers: Numbers can be negative, positive, and zero or decimal numbers. The data type used to store numbers such as short, int, and double can take 2 to 8 bytes of space in memory.
- Text: Text includes characters, alphabets, numbers, and a collection of them. Text can be of character or string type. The size of 1 character is 2 bytes.
Logical and Boolean Operators
In programming, boolean operators are logical operators(AND, OR, and NOT) that are symbols that allow you to combine or modify conditions to make logical evaluations. They are utilized to perform logical operations on boolean values (true or false). They are used to control the flow of a program.
There are three logical operators:
- Logical AND ( && ) Operator
- Logical OR ( || ) Operator
- Logical NOT ( ! ) Operator
1. Logical AND Operator ( && )
The logical AND operator (&&) is a binary operator that returns true only if both of its operands are true. Otherwise, if one of the operands is false then it returns false. Truth table for the AND operator is given below:
|
true
| true
| true
|
true
| false
| false
|
false
| true
| false
|
false
| false
| false
|
Syntax of Logical AND
expression1 && expression2
Example of Logical AND
Below is the implementation of the above method:
C++
// C++ Program to illustrate the logical AND Operator
#include <iostream>
using namespace std;
int main()
{
// initialize variables
int age = 25;
bool isStudent = true;
// Using AND operator in if condition
if (age > 18 && isStudent) {
cout << "You are eligible for a student discount."
<< endl;
}
else {
cout << "You are not eligible for a student "
"discount."
<< endl;
}
return 0;
}
C
#include <stdio.h>
#include <stdbool.h>
int main() {
// initialize variables
int age = 45;
bool isStudent = false;
// Using AND operator in if condition
if (age > 18 && isStudent) {
printf("You are eligible for a student discount.\n");
}
else {
printf("You are not eligible for a student discount");
}
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
// initialize variables
int age = 23;
boolean isStudent = true;
// Using AND operator in if condition
if (age > 18 && isStudent) {
System.out.println("You are eligible for a student discount.\n");
}
else {
System.out.println("You are not eligible for a student discount");
}
}
}
Python3
# initialize variables
age = 23
isStudent = True
# Using AND operator in if condition
if age > 18 and isStudent:
print("You are eligible for a student discount.")
else:
print("You are not eligible for a student discount")
JavaScript
// initialize variables
let age = 23;
let isStudent = true;
// Using AND operator in if condition
if (age > 18 && isStudent) {
console.log("You are eligible for a student discount.\n");
}
else {
console.log("You are not eligible for a student discount");
}
OutputYou are eligible for a student discount.
Explaination: In the code, we have used AND operator to check whether a person is eligible for a discount or not. So, we check if person's age is greater than 18 and the person is a student. If a person's age is greater then 18 and also a student the condition became true, the message "You are eligible for a student discount." will be printed. Otherwise, the else statement is executed.
2. Logical OR Operator ( || )
The Logical OR operator ( || ) is a binary operator that returns true if at least one of its operands is true. False will be returned only if both the operends are false. Here's the truth table for the OR operator:
|
true
| true
| true
|
true
| false
| true
|
false
| true
| true
|
false
| false
| false
|
Syntax of Logical OR
expression1 || expression2
Example of Logical OR
Below is the implementation of the above method:
C++
// C++ program to demonstrate the logical or operator
#include <iostream>
using namespace std;
int main()
{
int num = 7;
// using logical or for conditional statement
if (num <= 0 || num >= 10) {
cout
<< "The number is outside the range of 0 to 10."
<< endl;
}
else {
cout << "The number is between 0 to 10." << endl;
}
return 0;
}
C
#include <stdio.h>
int main() {
int num = 7;
// using logical or for conditional statement
if (num <= 0 || num >= 10) {
printf("The number is outside the range of 0 to 10.");
}
else {
printf("The number is between 0 to 10.");
}
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
int num = 7;
// using logical or for conditional statement
if (num <= 0 || num >= 10) {
System.out.println("The number is outside the range of 0 to 10.");
}
else {
System.out.println("The number is between 0 to 10.");
}
}
}
Python3
num = 7
# using logical or for conditional statement
if (num <= 0 or num >= 10):
print("The number is outside the range of 0 to 10.")
else:
print("The number is between 0 to 10.")
JavaScript
let num = 7;
// using logical or for conditional statement
if (num <= 0 || num >= 10) {
console.log("The number is outside the range of 0 to 10.");
}
else {
console.log("The number is between 0 to 10.");
}
OutputThe number is between 0 to 10.
Explaination: In the code, the condition num < 0 || num > 10 checks whether the number is either less than equal to 0 or greater than equal to 10. If either of these conditions is true, the message "The number is outside the range of 0 to 10." will be printed otherwise else statement is printed.
3. Logical NOT Operator ( ! )
The logical NOT operator ( ! ) is a unary operator that is used change the boolean value. It returns true if the condition is false, and false if the condition is true. Here's the truth table for the NOT operator:
Syntax of Logical NOT
! expression
Example of Logical NOT
Below is the implementation of the above method:
C++
// C++ program to illustrate the logical not operator
#include <iostream>
using namespace std;
int main()
{
bool isLoggedIn = false;
// using logical not operator
if (!isLoggedIn) {
cout << "Please log in to access this feature."
<< endl;
}
else {
cout << "Welcome to GeeksforGeeks!" << endl;
}
return 0;
}
C
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isLoggedIn = false;
// using logical not operator
if (!isLoggedIn) {
printf("Please log in to access this feature.");
}
else {
printf("Welcome to GeeksforGeeks!");
}
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
boolean isLoggedIn = false;
// using logical not operator
if (!isLoggedIn) {
System.out.println("Please log in to access this feature.");
}
else {
System.out.println("Welcome to GeeksforGeeks!");
}
}
}
Python3
isLoggedIn = False;
# using logical not operator
if not(isLoggedIn):
print("Please log in to access this feature.")
else:
print("Welcome to GeeksforGeeks!")
JavaScript
let isLoggedIn = false;
// using logical not operator
if (!isLoggedIn) {
console.log("Please log in to access this feature.");
}
else {
console.log("Welcome to GeeksforGeeks!");
}
OutputPlease log in to access this feature.
Explanation: In the code, the condition '!isLoggedIn' checks whether the user is not logged in. If the condition is true (i.e., the user is not logged in), the message "Please log in to access this feature." will be displayed otherwise else statement will be printed.
Relational and Boolean Operators
Relational operators are used to determine the relations between two values or expressions, and based on this comparison, it returns a boolean value (either true or false) as the result.
Syntax:
operand1 relational_operator operand2
expression1 relational_operator expression2
Types of Relational Operators
We have six relational operators which are explained below with examples.
|
>
| Greater than
| (a > b) If 'a' is greater than 'b' it gives true otherwise false.
|
<
| Less than
| (a < b) If 'a' is less than 'b' it gives true otherwise false.
|
>=
| Greater than equal to
| (a >= b) If 'a' is greater than or equal to 'b' it gives true otherwise false.
|
<=
| Less than equal to
| (a <= b) If 'a' is less than or equal to 'b' it gives true otherwise false.
|
==
| Equal to
| (a == b) If 'a' is equal to 'b' it gives true otherwise false.
|
!=
| Not equal to
| ( a != b) If 'a' is not equal to 'b' it gives true otherwise false.
|
Example of Relational and Boolean Operators
In the below code, we have defined two variables with some integer value and we have printed the boolean output by comparing them using relational operators. In the output, we get 1, 0, 0, 0, and 1 where 0 means false and 1 means true.
C++
// C++ Program to illustrate the relational operators
#include <iostream>
using namespace std;
int main()
{
// variables for comparison
int a = 10;
int b = 6;
// greater than
cout << "a > b = " << (a > b) << endl;
// less than
cout << "a < b = " << (a < b) << endl;
// equal to
cout << "a == b = " << (a == b) << endl;
// not equal to
cout << "a != b = " << (a != b) << endl;
return 0;
}
C
#include <stdio.h>
int main() {
// variables for comparison
int a = 10;
int b = 6;
// greater than
printf("a > b = %d\n", a > b);
// less than
printf("a < b = %d\n", a < b);
// equal to
printf("a == b = %d\n", a == b);
// not equal to
printf("a != b = %d", a != b);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
// variables for comparison
int a = 10;
int b = 6;
// greater than
System.out.println("a > b = "+ (a>b));
// less than
System.out.println("a < b = "+ (a<b));
// equal to
System.out.println("a == b = "+ (a==b));
// not equal to
System.out.println("a != b = "+ (a!=b));
}
}
Python3
# variables for comparison
a = 10;
b = 6;
# greater than
print("a > b =", (a > b));
# less than
print("a < b =", (a < b));
# equal to
print("a == b =", (a == b));
# not equal to
print("a != b =", (a != b));
JavaScript
// variables for comparison
let a = 10;
let b = 6;
// greater than
console.log("a > b =", a > b);
// less than
console.log("a < b =", a < b);
// equal to
console.log("a == b =", a == b);
// not equal to
console.log("a != b =", a != b);
Outputa > b = 1
a < b = 0
a == b = 0
a != b = 1
Conclusion
The Boolean data type, with its two fundamental values of true and false, lies at the heart of logical operations and decision-making in programming. Its simplicity and versatility make it an indispensable tool for developers across various programming languages and paradigms. By understanding how to work with Boolean data types and their associated logical operators, programmers can create more efficient, reliable, and powerful applications that can respond intelligently to different situations and user inputs.
Similar Reads
COBOL - Data Types
A Datatype is a classification by the programmer to tell the compiler/interpreter how data will be used inside a program. For example, the roll number of the student defined as the number will take input as a number only if other values are supplied instead of the number it will raise an abend insid
4 min read
Dart - Boolean
Dart language provides a pre-defined data type called boolean which can store two possible values, either true or false. To declare a boolean variable in Dart programming language, the keyword bool is used. Most commonly, boolean is used in decision-making statements. The syntax for declaring a bool
2 min read
C++ Data Types
Data types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ
7 min read
SAP ABAP | Data Types
Before Understanding the Data type first understand the Data object. Data objects are variables that we declare in the program. It occupies some memory where you can store the data from external sources. Data can be of different types, so data types are responsible for defining the type of data of t
6 min read
ES6 Boolean
The Boolean of ES6 is nothing different than the other Boolean objects. It also represents two values True or False. There are two properties and three methods that build this object in ES6 JavaScript. Properties of Boolean Object: JavaScript constructor: In ES6 JavaScript, the constructor property
3 min read
JavaScript Boolean
To represent logical values, JavaScript uses the Boolean data type, which has two possible values: true or false. These values often result from comparisons or logical operations. Additionally, the Boolean() function can convert other types of values into Boolean, determining their truthy or falsy n
4 min read
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc.Example:C++int number;The above statement declares a variable with name number that can store integer values.C is a statically type language where
5 min read
Lodash _.isBoolean() Method
Lodash _.isBoolean() method checks if the given value can be classified as a Boolean value or not.Syntax:_.isBoolean(value);Parameters: value: This parameter holds the value that needs to be Checked for Boolean.Return Value: This method returns a Boolean value(Returns true if the given value is a bo
2 min read
C++ Compound Data Types Quiz
Built-in data types cannot store all the information in an easily accessible and organized way. That is why C++ provides compound data types such as arrays, pointers, strings, etc. that are derived from the built-in data types and provide different way to use them. Good understanding of compound dat
2 min read
C++ Booleans
The ISO/ANSI C++ Standard has added certain new data types to the original C++ specifications. They are provided to provide better control in certain situations as well as for providing conveniences to C++ programmers. A boolean data type is declared with the bool keyword and can only take the value
2 min read