The Boolean data type represents logical values - True (1) or False (0) - and typically occupies 1 byte of memory. Any non-zero value is treated as True, while 0 is False. Booleans are essential in controlling program flow, especially in decision-making and conditional statements.
Example of Declaration of Boolean Data Type
Below are examples showing how to declare Boolean data types in different programming 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);
}
}
Python
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:
- Booleans: Represent logical values - True (1) or False (0) - typically occupy 1 byte of memory.
- Numbers: Include integers and floating-point values, which may use 2–8 bytes depending on type and system.
- Text: Consists of characters or strings; each character usually occupies 2 bytes.
Boolean Operators
Boolean operators perform logical operations on Boolean values to control program flow. The three primary logical operators are:
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:
Operand 1 | Operand 2 | Result |
|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Syntax:
expression1 && expression2
Example:
C++
#include <iostream>
int main() {
int age = 23;
bool isStudent = true;
if (age > 18 && isStudent) {
std::cout << "You are eligible for a student discount." << std::endl;
} else {
std::cout << "You are not eligible for a student discount." << std::endl;
}
return 0;
}
C
#include <stdio.h>
int main() {
int age = 23;
int isStudent = 1;
if (age > 18 && isStudent) {
printf("You are eligible for a student discount.");
} else {
printf("You are not eligible for a student discount.");
}
return 0;
}
Java
public class Main {
public static void main(String[] args) {
int age = 23;
boolean isStudent = true;
if (age > 18 && isStudent) {
System.out.println("You are eligible for a student discount.");
} else {
System.out.println("You are not eligible for a student discount.");
}
}
}
Python
age = 23
isStudent = True
if age > 18 and isStudent:
print("You are eligible for a student discount.")
else:
print("You are not eligible for a student discount.")
JavaScript
let age = 23;
let isStudent = true;
if (age > 18 && isStudent) {
console.log("You are eligible for a student discount.");
} else {
console.log("You are not eligible for a student discount.");
}
OutputYou are eligible for a student discount.
Explaination:
- "if condition" checks if the user is older than 18 and a student.
- Both are True, so the if block executes.
2. Logical OR Operator ( || )
It's a binary operator which returns True if at least one operand is True. Below is the truth table:
Operand 1 | Operand 2 | Result |
|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Syntax:
expression1 || expression2
Example:
C++
#include <iostream>
int main() {
int num = 7;
if (num <= 0 || num >= 10) {
std::cout << "The number is outside the range of 0 to 10." << std::endl;
} else {
std::cout << "The number is between 0 to 10." << std::endl;
}
return 0;
}
C
#include <stdio.h>
int main() {
int num = 7;
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
public class Main {
public static void main(String[] args) {
int num = 7;
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.");
}
}
}
Python
num = 7
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;
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:
- if condition checks if num is ≤ 0 or ≥ 10.
- Both conditions are False, so the else block execute
3. Logical NOT Operator ( ! )
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. Truth table is given below:
Operand 1 | Result |
|---|
true | false |
false | true |
Syntax:
! expression
Example:
C++
#include <iostream>
int main() {
bool isLoggedIn = false;
if (!isLoggedIn) {
std::cout << "Please log in to access this feature." << std::endl;
} else {
std::cout << "Welcome to GeeksforGeeks!" << std::endl;
}
return 0;
}
C
#include <stdio.h>
int main() {
int isLoggedIn = 0;
if (!isLoggedIn) {
printf("Please log in to access this feature.");
} else {
printf("Welcome to GeeksforGeeks!");
}
return 0;
}
Java
public class Main {
public static void main(String[] args) {
boolean isLoggedIn = false;
if (!isLoggedIn) {
System.out.println("Please log in to access this feature.");
} else {
System.out.println("Welcome to GeeksforGeeks!");
}
}
}
Python
isLoggedIn = False
if not isLoggedIn:
print("Please log in to access this feature.")
else:
print("Welcome to GeeksforGeeks!")
JavaScript
let isLoggedIn = false;
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:
- "not isLoggedIn" inverts False to True.
- Hence, the if block runs, asking the user to log in.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice