C++

C++ Boolean Type

C++ is the high-level OOP language that is used to design for various purposes. It makes programming more enjoyable for the programmers. When working in C++, we sometimes need a result in true or false form, so we utilize the “Boolean data type”. In C++, a Boolean is a kind of datatype that can represent either true or false results. Programmers use it frequently to analyze the conditions, make decisions, or regulate the program execution. Boolean datatype is frequently employed to ascertain whether a given condition is true or false. For this “Boolean data type,” we utilize the “Bool” keyword in C++. Here, we will study about the “Boolean data type” in detail and check how this Boolean data type works in C++.

Example 1:

Now, let’s perform some examples where we utilize this “Boolean data type” and show it’s working in C++. We start our code by adding the header files that we require. The first header file that we add here is the “<iostream>” which aids in inputting or outputting the data. After this, we have the “std” namespace; this is the standard name.

After this, we have a driver code which means that we add the “main()” function here. Now, we declare the “isBulbOn” variable with the Boolean datatype “bool” and adjust “true” here. Below this, we have another Boolean variable named “isBulbOff” in which we add “false”. This true and false result are “1” and “0”, respectively.

To check the output of these Boolean values, we simply print them using the “cout” statement. In this “cout” statement, we first print the “isBulbOn” result. Then, in the next line, we print the result of the “isBulbOff” variable. The “endl” is utilized here so it moves our pointer to the next line.

Code 1:

#include <iostream>
using namespace std;
int main()
{
    bool isBulbOn = true;
bool isBulbOff = false;
cout << "The bulb is on here "<< isBulbOn << endl;  
cout << "The bulb is not on here " << isBulbOff;
}

Output:

The output of this code represents the result in the “0” and “1” forms as shown in the following. Here, “1” indicates the “true” result while “0” indicates the “false” result. We obtain this result just because of the “bool” data type.

Example 2:

Now, we declare two variables, “Pass” and “Fail”, of the “bool” datatype inside the main after including the header file at the start of this code. The “Pass” variable is assigned as “true” here, and the “Fail” variable is assigned as “false”. Now, “Pass” returns “1” as a result and “Fail” returns “0”.

Now, we utilize these bool variables in our “cout” statement to get the true or false result in the form of “1” and “0”. The “cout” where we put “Pass” returns “1”. Where we utilize “Fail” returns “0”. Here, we add five “cout” statements, each contains the Boolean variable.

Code 2:

#include <iostream>
using namespace std;
int main()
{
    bool Pass = true;
bool Fail = false;
cout << "The percentage is 60 "<< Pass << endl;
cout << "The percentage is 45 " << Fail << endl;
cout << "The percentage is 90 " << Pass << endl;
cout << "The percentage is 85 " << Pass << endl;
cout << "The percentage is 33 " << Fail << endl;
}

Output:

In this output, “1” represents the “true” result which is “Pass” and “0” represents the “false” result which is “Fail” in this case.

Example 3:

In this code, we initialize three integer variables which are “num_01”, “num_02”, and “a” with the value of “45”, “62”, and “3”, respectively. After this, we declare three more variables – “b_01”, “b_02”, and “b_03” – and these are the Boolean datatype “bool”. Now, we initialize “b_01” with the “num_01 == num_01” condition. Then, we initialize “b_02” and “b_03” in the same ways as “b_01”.

After initializing all variables, we print them separately using “cout” to check the result of these Boolean variables. After this, we initialize the “b_a” variable of the “bool” data type with “true”. Then, we utilize the “if” statement here where we place “b_a” as a condition. Now, if this “b_a” condition is true, the statement after “if” is executed. Otherwise, the “else” part will execute here. After this, we proceed and initialize the “num” integer variable in which we apply some mathematical operations and display the “num” result.

Code 3:

#include <iostream>
using namespace std;
int main()
{
int num_01 = 45, num_02 = 62, a = 3;
bool b_01, b_02, b_03;
b_01 = num_01 == num_01;
b_02 = num_01 == num_02;
b_03 = num_02 > num_01;

cout << "The answer of first Bool b_01 is = " <<
        b_01 << endl;
cout << "The answer of second Bool b_02 is = " <<
        b_02 << endl;
cout << "The answer of third Bool b_03 is = " <<
        b_03 << endl;
bool b_a = true;
if (b_a)
    cout << "Yes" << endl;
else
    cout << "No" << endl;
int num = false + 7 * a - b_a + true ;
cout << num;
return 0;
}

Output:

This outcome shows the results of the operations that we executed in our code. So, in this way, we use this “Boolean data type” in our C++ codes.

Example 4:

Here, we type “isHotDay” as a “bool” variable and initialize it with “false”. Now, we use the “if” statement and pass “isHotDay” as the condition. The statement that follows “if” is now executed if the “isHotDay” condition is satisfied. Otherwise, the “else” portion will run at this point.

Now, we have the “DoTask” Boolean variable and set it to “true”. Moreover, we also initialize the “int” variable named “Task_count”. After this, we place the “while()” loop. In this “while()” loop, we put “DoTask” as the condition. Inside the while loop, we write “Task_count++” which increments the value of “Task_count” by 1.

When this statement is executed, the value of “Task_count” increases by 1. Then, the next “cout” statement is executed. After this, we place a condition again which is “Task_count < 9” and assign this condition to the “DoTask” variable. This loop works until the “Task_count” is less than “9”.

Code 4:

#include <iostream>  
using namespace std;  
int main() {  
    bool isHotDay = false;  
    if (isHotDay) {  
        cout << "It's a hot day!" << endl;  
    } else {  
        cout << "It's not a hot day" << endl;
    }  
    bool DoTask = true;  
    int Task_count = 0;  
    while (DoTask) {  
        Task_count++;  
        cout << "Task is continue here " << Task_count << endl;  
        DoTask = (Task_count < 9);  
    }  
    return 0;  
}

Output:

This output displays the result of every action that we ran through our code. Thus, we also employ this “Boolean data type” in our C++ codes in this manner.

Example 5:

Now, we move towards the last example of this tutorial. Here, we take three unique Boolean variables and print both. After this, we apply the “AND”, “OR”, and “NOT” operators on these Boolean variables. Also, the result of all operations is stored in Boolean form because we added “bool” with all the variables in which the result of these operations is stored. After this, we print the outcome of these operations again in Boolean.

Code 5:

#include<iostream>
using namespace std;
int main()
{
    bool value_1 = true;  
bool value_2 = false;  
bool value_3 = true;  

cout<< "value_1 is " << value_1 <<endl;
cout<< "value_2 is " << value_2 <<endl;
cout<< "value_3 is " << value_3 <<endl<<endl;

bool outcome_1 = (value_1 || value_3) && value_1;
bool outcome_2 = value_1 && value_2;  
bool outcome_3 = value_2 || value_3;  
bool outcome_4 = !value_3;    
bool outcome_5 = !value_2;
bool outcome_6 = !value_1;    

cout << "The result 1 is = " << outcome_1 << endl;
cout << "The result 2 is = " << outcome_2 << endl;
cout << "The result 3 is = " << outcome_3 << endl;
cout << "The result 4 is = " << outcome_4 << endl;
cout << "The result 5 is = " << outcome_5 << endl;
cout << "The result 6 is = " << outcome_6 << endl;
}

Output:

Here is the outcome. We might notice that the result of each operation is displayed in the form of “0” and “1” because the “bool” data type is utilized.

Conclusion

In this tutorial, we demonstrated how the Boolean data type is utilized in C++ and what the outcome of the Boolean data type is. We explored the examples in which we used this Boolean data type. We have seen that this Boolean data type is effective and straightforward, but it is essential to utilize it carefully to prevent mistakes.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.