BOOL DATATYPE IN C++
Introduction:
The original C++ requirements include several new data types added to them by the ISO/ANSI C++ Standard. They are offered to C++ developers for efficiency as well as to provide them more power in specific scenarios. The bool keyword is used to specify a Boolean data type, which can only accept values in the true or false format. Bool is one of the newly available data types. In C++, Boolean is a kind of data that may represent either true or false values. Programming uses it often to analyze situations, make decisions, and regulate program execution. A Boolean data type in C++ is one that has two possibility values which is either true or false. To find out whether a given condition is true or false, Booleans are frequently employed in loops, conditional statements, along with various control structures.
Characteristics of Bool datatype:
- The terms true and false in C++ stand for Booleans.
- They have a maximum size of one byte, and they are stored in memory as either 1 (true) or 0 (false).
- For creating more complicated logical expressions, Booleans are combined with logical operators like && (and), || (or), and ! (not).
Syntax for declaring bool datatype:
bool variable_name = true; (variable is assigned with true value)
bool variable_name = false; (Variable is assigned with a false value)
Logical operators are available to combine boolean values to generate more complicated conditions. In C++, there are three types of logical operators:
- || (logical OR)
- && (logical AND)
- ! (logical NOT)
If the two of its variables are true, the logical AND function returns true. If any one of its variables are true, the logical OR function returns true. The numerical value of the variable is reversed by the logical NOT operator.
Examples:
bool value1 = false;
bool value2 = true;
bool value3 = true;
bool res1 = value1 && value2 //res1 will be false
bool res2 = value1 || value3 //res2 will be true
bool res3 = !value2 //res3 will be false
Program 1:
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 20, x = 5;
bool p, q;
p = a == b;
q = a < b;
cout << " Bool1 is : " << p << "\n";
cout << " Bool2 is : " << q << "\n";
bool r = true;
if (r)
{
cout << " Yes" << "\n";
}
else
{
cout << " No" << "\n";
}
int c = false + 4 * x - r;
cout << c;
return 0;
}
Output:
Program 2:
#include <iostream>
using namespace std;
int main()
{
bool is_rainy = true;
if (is_rainy)
{
cout << "It's a rainy day" << endl;
} else
{
cout << "It is not a rainy day" << endl;
}
bool should_Continue = true;
int count = 0;
while (should_Continue)
{
count++;
cout << "Count is updated to " << count << endl;
should_Continue = (count < 5);
}
return 0;
}
Output:
Explanation:
- The program initially declares and then initializes the Boolean variable named is_rainy to true in the above instance. The value of is_rainy is then checked using an if statement, and based on either if it is true or false, an output is sent to the console.
- Next, an integer variable count is assigned to 0, and an alternative Boolean variable should_Continue is declared and initialized to true. After that, a while loop is triggered, and it keeps going until should_Continue holds true.
- Within the loop, the code modifies the numerical value of should_Continue depending on the value of count, increases the value of count, and then writes the value of count into the console. The loop ends whenever count reaches 5 and should_Continue has been changed to false.
- Eventually, the program outputs 0 indicating its successful completion of the program.
Conclusion:
In C++ programming, booleans are an essential component that are widely utilized in logical expressions as well as in control structures. They are effective and simple to use, but it is important to utilize them carefully to prevent mistakes. Using Booleans may occasionally be confusing, particularly when paired with various other logical operators. When used incorrectly, they can also lead to mistakes. For example, when using = in a conditional expression instead of ==.