What Is Boolean Data Type Give Example With Program?

//

Angela Bailey

What Is Boolean Data Type? Give Example With Program

A boolean data type is a fundamental data type in programming that represents two possible values: true or false. It is named after mathematician and logician George Boole, who first defined an algebraic system of logic. In programming, the boolean data type is commonly used for making decisions or determining the state of a condition.

Example Program:

Let’s take a look at a simple example program to understand how the boolean data type works:

<!DOCTYPE html>
<html>
<head>
  <title>Boolean Example</title>
</head>
<body>

  <h3>Example Program: Checking if a number is even or odd</h3>

  <p id="result"></p>

  <script>
    // Define a variable
    var number = prompt("Enter a number:");

    // Check if the number is even
    var isEven = (number % 2 === 0);

    // Display the result
    if (isEven) {
      document.getElementById("result").innerHTML = "The number " + number + " is even.";
    } else {
      document.innerHTML = "The number " + number + " is odd.";
    }
  </script>

</body>
</html>

In this example program, we use the boolean data type to check if a given number is even or odd. The program prompts the user to enter a number and then calculates whether it’s divisible by 2 using the modulo operator (%).

If the result of the calculation is 0, it means the number is even, and the boolean variable “isEven” is set to true. Otherwise, it’s odd, and “isEven” is set to false.

Based on the value of “isEven”, we display a corresponding message using JavaScript. If “isEven” is true, we display a message stating that the number is even. If it’s false, we display a message stating that the number is odd.

Summary:

  • A boolean data type represents two possible values: true or false.
  • It is commonly used for making decisions or determining the state of a condition.
  • In JavaScript, true and false are keywords that represent boolean values.
  • We can use boolean variables to store and manipulate boolean data.
  • The example program demonstrated how to check if a number is even or odd using the boolean data type.

Understanding the boolean data type and how to use it in programming can greatly enhance your ability to make logical decisions within your programs. It’s an essential concept to grasp for anyone learning to code!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy