What is C++ booleans?

C++ booleans gives us only two value either true or false. To work with any programming language sometimes we need such variables which has the value of only true or false. These two value true or false is called boolean value. C++ supports booleans and we use them most for checking any condition. You will learn more in our condition chapter.

C++ booleans can only hold any one of  the two value true or false. These are also express by ‘yes or no’ as well as ‘0 and 1’. In this article we will learn and see some program of C++ booleans.

Value of booleans in C++

The value of true is one and false is zero. Let’s check this by following C++ program.

#include <iostream>
using namespace std;

int main(){
    bool x = true;
    bool y = false;

    cout << x << " and " << y << endl;

    return 0;
}

program output:

c-booleans-boolean-in-c

Here the program returns value 1 for true and 0 for false. We can check any expressions by this concept. Now, let’s check any expression that it is true or not.

Boolean expression checking

See the bellow program where we have used several boolean expressions to check boolean value.

// c++ boolean expressions

#include <iostream>
using namespace std;

int main(){
    int a = 7;
    int b = 9;

    cout << (a > b) << endl;    // false: return 0
    cout << (a < b) << endl;    // true: return 1

    cout << (10 > 3) << endl;    // true: return 1
    cout << (10 < 3) << endl;    // false: return 0

    cout << (10 == 3) << endl;    // false: return 0
    cout << (10 != 3) << endl;    // true: return 1

    return 0;
}

Output of these boolean expressions

0
1
1
0
0
1

You can also compare two characters or other types of data using Boolean expressions. We will learn more about Boolean expressions later in our if else condition chapter. Normally we use these Boolean expressions to control the flow of our program.

Previous pageMath functions in C++

Next pageType conversion in C++