C++

Bool Data Type C++

There are many data types found in programming languages used within the code. These data types are used according to the situation and calculation. One of the data types in C++ is the boolean data type used with the keyword “bool”. The bool data type can be initialized with only two values i.e. true and false. It doesn’t take any value other than that.

Within this article tutorial, we will be making use of bool data type in our examples of C++. As we are implementing our examples in Ubuntu 20.04 system, make sure to have the G++ compiler installed in your Linux system already. Let’s get started with some simple examples.

Example 01:

Within our first example of C++, we will see how the bool data type can be initialized and printed out in the terminal shell of the Ubuntu 20.04 system. So, open the shell terminal using the “Ctrl+Alt+T” shortcut or search it from the activity area. The terminal shell will be opened within 5 seconds. Now, use the old “touch” command to generate a new C++ file named bool.cc. This file will be found in the home folder.

Try to open it with an editor installed on your system to add code to it. We have been using the built-in GNU Nano editor here to open it directly within the terminal. The vim and text editors are also available in Ubuntu 20.04 system.

Now, the file has been opened in the nano editor for coding. Initially, this file will be empty. We have included the input-output header file to add input and output it on the shell. The standard namespace is necessary to be used in the code. Without it, our code will get errors. The main() function is initialized after that.

Without the main() function, no execution will take place. Within this main() method, we have initialized two boolean data type variables named v1 and v2. The variable v1 takes “true” and v2 takes “false” as its value. After the initialization, cout statements are being used here to display both the boolean variables within the shell separately.

The first thing you have to do is to compile the saved code file. So, the installed g++ compiler will be used for this purpose. After the compilation, the simple “./a.out” command will be used to run the file properly. The compilation got successful, and we have got the result as 1 and 0. This means that the standard cout statement takes true as 1 and false as 0.

Example 02:

Let’s see how the boolean values can be used within the mathematical expression. So, open the file “bool.cc” to update the code. The input-output stream and standard namespace lines are left unchanged. Within the main function of this code, we have initialized an integer variable “v”. This variable is taking two boolean expressions and 1 integer value to sum them up.

As we know that the true means 1 and false means 0, then the sum must be 8 here. After this, the cout statement is used to display the calculated value of the variable v.

Here comes the compilation with the g++ compiler. It gets successful and after running the code, we have got what we have expected i.e. 8 as a result.

Example 03:

The G++ compiler can convert many integer values to the boolean data type. Let’s have a new example to do that as well. So, we have opened the same file with the “nano” editor. After including the input-output stream header and standard namespace, we have used the main() function to use boolean. Within the main method, we have initialized 4 integer variables i.e. 0, positive, and negative integers. The bool data type has been assigned to all of them.

The g++ compiler takes 0 as false and all other integer values as true for the boolean data type. So, the cout statements are used here to separately display the result of all 4 variables.

Let’s compile the code with g++ first and see the execution result. The compilation is successful with the g++ compiler and the “./a.out” command shows the result as 0,1,1,1. It turns out as we expected. It converts 0 as false and all other values to true for the boolean data type.

Example 04:

Let’s see how the standard cout statement works on the boolean expression i.e. true or false in the terminal shell of the Ubuntu 20.04 system. So, we have opened the same bool.cc file to update the code. After adding the header file of the input-output stream and standard namespace, the main() function is added as a necessity. Within the main function, 4 standard cout statements have been used. All the statements are using boolean expressions i.e. true and false. Two of them are used as it is while the other two are used with the not “!” sign to make the revert of the original value.

The compilation got smooth with the g++ installer as there are no errors in the code. The execution returns the results as the cout statements mentioned in their texts.

Example 05:

Within this example, we will be getting the boolean value from a user as input and displaying the result on the console shell. So, we will be making use of the same file i.e. “bool.cc”. Within the main() function of this code, we have initialized a boolean type array variable named “a” with no size defined. The default initialization of this boolean variable would be taken as false.

The cout statement is used to tell the user to enter some boolean value on the shell. The “cin” standard statement has been used to get the inputted value from the shell and saved to the variable “a”. The next standard cout statement will be displayed if the added value is true or false.

After adding the “false” and “true” on the shell as input we have got 0. This means any string value would be considered as a value other than a boolean value.

If you want to get the correct output for boolean values, you have to add 1 and 0 as true and false. So, we did the same and got the correct result as below.

Example 06:

Let’s end this article with the bonus example to compare two values to get a boolean result in return. So, we have utilized the same file for this example. Within the main() function of this file, we have initialized two integer type variables i.e. x and y having the same value i.e. 42. The cout statement has been used to display the comparison result of the comparison expression used within it.

The execution of this code returns 1 as the comparison expression returns true.

Conclusion:

Within this guide, we have discussed the boolean data type and its use within the C++ language. Other than that, we have also discussed the conversion of integer values to boolean and the use of boolean values within the mathematical expressions. The article has utilized the examples to get boolean input from the user and make use of comparison expressions as well. In the end, we hope this article will be helpful to every naive and expert user in C++.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.