Open In App

Boolean Data Type

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


Output

a: 1
b: 0
c: 1

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:

  1. Logical AND ( && ) Operator
  2. Logical OR ( || ) Operator
  3. 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:

Operand 1

Operand 2

Result

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");             
}


Output

You 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:

Operand 1

Operand 2

Result

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.");
}


Output

The 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:

Operand 1

Result

true

false

false

true

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!");
}


Output

Please 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.

S. No.

Relational Operator

Meaning

Example

1

>

Greater than

(a > b) If ‘a’ is greater than ‘b’ it gives true otherwise false.

2

<

Less than

(a < b) If ‘a’ is less than ‘b’ it gives true otherwise false.

3

>=

Greater than equal to

(a >= b) If ‘a’ is greater than or equal to ‘b’ it gives true otherwise false.

4

<=

Less than equal to

(a <= b) If ‘a’ is less than or equal to ‘b’ it gives true otherwise false.

5

==

Equal to

(a == b) If ‘a’ is equal to ‘b’ it gives true otherwise false.

6

!=

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);


Output

a > 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads