Boolean Values

We said that all conditions are ultimately either true (true) or false (false). We can verify this by storing the result of a condition in a variable or printing it:

// Printing the result of a condition
std::cout << (10 < 20) << std::endl;    // Prints: 1 (true)
std::cout << (10 == 20) << std::endl;   // Prints: 0 (false)

// Storing in a variable
bool condition = (5 > 3);     // true
std::cout << condition << std::endl;   // Prints: 1

The values true (truth) and false (falsehood) are called Boolean values. You might have noticed that when we print these values, it doesn't print true or false, but 1 or 0. We just need to know that 0 means false and 1 means true.

We can also use Boolean values directly in code:

bool always = true;
if (always) {
    std::cout << "This will always print" << std::endl;
}

bool never = false;
if (never) {
    std::cout << "This will never print" << std::endl;
}

We can also compare Boolean values with each other:

std::cout << (true == true) << std::endl;    // Prints: 1
std::cout << (true == false) << std::endl;   // Prints: 0
std::cout << (true != false) << std::endl;   // Prints: 1

Even though this may not seem very useful now, you will later see that Boolean values are a fundamental building block of programming.

Instructions

Complete the condition so that the text is printed only if the values of variables a and b are different.

Hint: use the != operator for comparison.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

5/10

Boolean Values | Start Coder