Boolean in C++

In this chapter, we will learn about Boolean data types in C++.

ON and OFF can be something fun! right?, in programming we have data type than can help you play like that.

The Boolean variable are use to store one of two values like:

  • ON / OFF
  • True / False
  • Yes / No

To perform the above operations C++ has booldata type that can store true which is (1) or falsewhich is also (0).

The boolean variable also could be referred as logical variable.

The data type of boolean is bool and it was name after the George Boole who invented boolean algebra, and it regarded as integer type.

Boolean Values

We use this variable when we want to stored the value of something that can eaither be trueor false.

// Boolean Operations
#include <iostream>
using namespace std;

int main(){
  bool isCodingFun = true;
  bool isAgeEnough = false;

  cout << isCodingFun << endl; // Outputs 1 (true)
  cout << isAgeEnough << endl; // Outputs 0 (false)

  return 0;
}

When you observed the above example you can deduct that the true value returns 1 while the false value returns 0.

Boolean Expression

The Boolean Expression also returns a boolean value that can be trueor false.

Let's say you want to create logic in your code and display the answer, then this is very usefull.

And to achieve the above logic you need a Comparative Operators like greater than >operator, to find out if an expression is eaither trueor false.

Let's look at this example:

// Boolean Operations
#include <iostream>
using namespace std;

int main(){
  int a = 8;
  int b = 5;

  cout << (a > b) << endl; // returns 1 (true), because 8 is higher than 5
 
  return 0;
}

Or you can place the logic inside then output statement direct like this:

// Boolean Operations
#include <iostream>
using namespace std;

int main(){

  cout << (8 > 5) << endl; // returns 1 (true), because 8 is higher than 5

  return 0;
}

We can also use equal to (==) operators to check if the variables or values are thesame.

// Boolean Operations
#include <iostream>
using namespace std;

int main(){
  int c = 10;
  cout << (c == 10) << endl; // returns 1 (true), because the value of c is equal to 10
  cout << (10 == 15) << endl; // returns 0 (false), because 10 is not equal to 15

  return 0;
}

The stage is yours now go and practice to understand how it works.

Real World Example

Learning new things can be tedious right? especiall on how to implement what I have just learned in real world example.

Now let take a example of Club gate where only people above 18 years is allowed to enter. So we can use boolean to check if the person is above 18 or not.

// Boolean Operations
#include <iostream>
using namespace std;

int main(){
  int myAge = 25;
  int age = 18;
  bool isAge = myAge >= age; // check if my age is greater than or equal to 18
  cout << isAge << endl; // returns 1 (true), meaning I am allowed to enter!

  return 0;
}

The above example we use >= comparison operator to check if myAge which is set to be 25 is greater than or equal to the entrance age which is set to be 18.

Fun right! it will be een fun if we wrap it in if...else statement, so that we can perform different action based on the result.

// Boolean Operations
#include <iostream>
using namespace std;

int main(){
  int myAge = 25;
  int age = 18;
  
  if(myAge >= age){
    cout << "You are allowed to enter" << endl;
  }else{
    cout << "You are not allowed to enter" << endl;
  }

  // Output: You are allowed to enter

  return 0;
}

The boolean expression are very usefull for all comparisons and conditions statements.

You will learn more about Conditions (if...else) in next chapter.