DEV Community

Cover image for Boolean Expressions, If Statements, and the Ternary Operator in C++
Tomáš Svojanovský
Tomáš Svojanovský

Posted on

Boolean Expressions, If Statements, and the Ternary Operator in C++

Boolean logic is one of the most basic but important parts of programming. In C++, boolean expressions are used whenever we need to make decisions in our code.

If you already know another programming language, most of this will feel familiar. C++ uses the same basic ideas: true, false, comparison operators, if statements, else if, else, and the ternary operator.

Let’s go through them step by step.

Boolean Values in C++

C++ has a built-in boolean type called bool.

A bool variable can only store one of two values:

bool b1 = true;
bool b2 = false;
Enter fullscreen mode Exit fullscreen mode

Both true and false are keywords in C++, and they are written in lowercase.

This is important. For example, this is correct:

bool isReady = true;
Enter fullscreen mode Exit fullscreen mode

But this is not valid C++:

bool isReady = True; // wrong
Enter fullscreen mode Exit fullscreen mode

Boolean Expressions

A boolean expression is an expression that evaluates to either true or false.

For example:

bool b1 = true;
bool b2 = false;
Enter fullscreen mode Exit fullscreen mode
bool c1 = b1 == b2;
Enter fullscreen mode Exit fullscreen mode

Here, b1 == b2 checks whether b1 and b2 have the same value.

Since b1 is true and b2 is false, the result is false.

So c1 stores:

false
Enter fullscreen mode Exit fullscreen mode

C++ supports the usual comparison operators:

==  // equal to
!=  // not equal to
<   // less than
>   // greater than
<=  // less than or equal to
>=  // greater than or equal to
Enter fullscreen mode Exit fullscreen mode

You can also combine boolean expressions using logical operators:

&&  // and
||  // or
!   // not

Enter fullscreen mode Exit fullscreen mode

Example:

bool b1 = true;
bool b2 = false;

bool result = b1 || b2;
Enter fullscreen mode Exit fullscreen mode

This means:

true OR false
Enter fullscreen mode Exit fullscreen mode

So the result is true.

Use Parentheses for Clarity

In many programming languages, including C++, operator precedence determines which parts of an expression are evaluated first.

For simple expressions, this is usually obvious. But once you combine multiple conditions, the code can become harder to read.

For example:

bool result = b1 || b2 && !b1;
Enter fullscreen mode Exit fullscreen mode

This works, but it is not immediately clear what the programmer intended.

A clearer version would be:

bool result = b1 || (b2 && !b1);
Enter fullscreen mode Exit fullscreen mode

Or maybe this was intended:

bool result = (b1 || b2) && !b1;
Enter fullscreen mode Exit fullscreen mode

These two expressions are different.

That is why it is usually a good idea to add parentheses when combining conditions. The goal is not just to make the compiler understand the code, but also to make the code understandable for humans.

If Statements

The most common place where boolean expressions are used is inside if statements.

Example:

#include <iostream>

int main()
{
    bool b1 = true;

    if (b1 == true)
    {
        std::cout << "Printing\n";
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

The condition inside the parentheses is evaluated.

If the condition is true, the code inside the curly braces runs.

If the condition is false, the code block is skipped.

In this example, b1 == true is true, so the program prints:

Printing
Enter fullscreen mode Exit fullscreen mode

Shorter Boolean Checks

When a variable is already a boolean, you do not need to compare it explicitly to true.

Instead of writing this:


if (b1 == true)
{
    std::cout << "Printing\n";
}

Enter fullscreen mode Exit fullscreen mode

You can write:

if (b1)
{
    std::cout << "Printing\n";
}
Enter fullscreen mode Exit fullscreen mode

This is shorter and more idiomatic.

Both versions mean the same thing:

if b1 is true, run this block

Similarly, instead of writing:

if (b1 == false)
{
    std::cout << "Not printing\n";
}
Enter fullscreen mode Exit fullscreen mode

You can write:

if (!b1)
{
    std::cout << "Not printing\n";
}
Enter fullscreen mode Exit fullscreen mode

The ! operator means “not”.

So:

!b1
Enter fullscreen mode Exit fullscreen mode

means:

not b1
Enter fullscreen mode Exit fullscreen mode

If b1 is true, then !b1 is false.

If b1 is false, then !b1 is true.

Else If and Else

C++ also supports else if and else.

Example:

#include <iostream>

int main()
{
    bool b1 = false;
    bool b2 = false;

    if (b1)
    {
        std::cout << "b1 is true\n";
    }
    else if (!b2)
    {
        std::cout << "b2 is false\n";
    }
    else
    {
        std::cout << "No condition matched\n";
    }

    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Here is what happens:

First, C++ checks if (b1).
If that is false, it checks else if (!b2).
If that is also false, it runs the else block.

In this example, b1 is false and b2 is also false.

So:

!b2
Enter fullscreen mode Exit fullscreen mode

is true.

The output will be:

b2 is false
Enter fullscreen mode Exit fullscreen mode

Curly Braces in If Statements

In C++, curly braces define the body of an if, else if, or else block.

This is the recommended style:


if (b1)
{
    std::cout << "b1 is true\n";
}
Enter fullscreen mode Exit fullscreen mode

Technically, if the body contains only one statement, you can omit the curly braces:

if (b1)
    std::cout << "b1 is true\n";
Enter fullscreen mode Exit fullscreen mode

This is valid C++.

However, it can easily become dangerous when you add more lines later.

For example:

if (b1)
    std::cout << "b1 is true\n";
    std::cout << "This always runs\n";
Enter fullscreen mode Exit fullscreen mode

Only the first line belongs to the if statement.

The second line always runs, because it is not inside a block.

That is why many developers prefer to always use curly braces, even for one-line if statements:


if (b1)
{
    std::cout << "b1 is true\n";
    std::cout << "This only runs if b1 is true\n";
}
Enter fullscreen mode Exit fullscreen mode

This style is safer and easier to maintain.

Compiler Warnings and Unused Variables

When writing code like this:

bool b1 = true;
bool b2 = false;

bool c1 = b1 == b2;
Enter fullscreen mode Exit fullscreen mode

you may see a compiler warning if c1 is never used.

The warning may say something like:

unused variable 'c1'
Enter fullscreen mode Exit fullscreen mode

or:

local variable is initialized but not referenced
Enter fullscreen mode Exit fullscreen mode

This means that you created a variable, stored a value in it, but never used it later.

For example:

bool c1 = b1 == b2;
Enter fullscreen mode Exit fullscreen mode

If c1 is not used anywhere else, then the variable is unnecessary.

Compiler warnings are useful because they help you detect code that may be redundant, wasteful, or incorrect.

In modern C++ development, it is common to enable many compiler warnings. They help you catch problems early.

The Ternary Operator

Sometimes you want to assign one value if a condition is true, and another value if the condition is false.

You can do this with a normal if statement:


#include <cstdint>

int main()
{
    bool b1 = true;

    std::int32_t result;

    if (b1)
    {
        result = 10;
    }
    else
    {
        result = -10;
    }

    return 0;
}

Enter fullscreen mode Exit fullscreen mode

This works perfectly fine.

But C++ also gives us a shorter way to write this: the ternary operator.

std::int32_t result = b1 ? 10 : -10;
Enter fullscreen mode Exit fullscreen mode

The structure is:

condition ? value_if_true : value_if_false
Enter fullscreen mode Exit fullscreen mode

So this:

std::int32_t result = b1 ? 10 : -10;
Enter fullscreen mode Exit fullscreen mode

means:

if b1 is true, store 10
otherwise, store -10

If b1 is true, then result becomes 10.

If b1 is false, then result becomes -10.

Ternary Operator vs If Statement

The ternary operator is useful when you want to choose between two values.

For example:

std::int32_t result = b1 ? 10 : -10;
Enter fullscreen mode Exit fullscreen mode

This is much shorter than:

std::int32_t result;

if (b1)
{
    result = 10;
}
else
{
    result = -10;
}
Enter fullscreen mode Exit fullscreen mode

However, the ternary operator should not be overused.

This is readable:

std::int32_t result = b1 ? 10 : -10;
Enter fullscreen mode Exit fullscreen mode

But this is much harder to understand:

std::int32_t result = b1 ? 10 : b2 ? 20 : b3 ? 30 : -10;
Enter fullscreen mode Exit fullscreen mode

Nested ternary expressions can quickly become difficult to read.

In that case, an if, else if, else structure is usually better:


std::int32_t result;

if (b1)
{
    result = 10;
}
else if (b2)
{
    result = 20;
}
else if (b3)
{
    result = 30;
}
else
{
    result = -10;
}
Enter fullscreen mode Exit fullscreen mode

This is longer, but it is much clearer.

When Should You Use the Ternary Operator?

Use the ternary operator when:

  • you are choosing between two simple values
  • the condition is easy to read
  • the whole expression stays short

Good example:

std::int32_t result = isPositive ? 1 : -1;
Enter fullscreen mode Exit fullscreen mode

Avoid the ternary operator when:

  • you need multiple branches
  • the expression becomes too long
  • you are doing more than just selecting a value
  • readability suffers

Bad example:

std::int32_t result = a > b ? x + y * z : c < d ? p - q : r / s;
Enter fullscreen mode Exit fullscreen mode

This may compile, but it is not pleasant to read.

Summary

Boolean expressions are used to control the flow of a program.

In C++, a bool can store either true or false. You can use comparison operators and logical operators to build conditions, and those conditions can be used inside if, else if, and else statements.

The ternary operator is a shorter alternative to a simple if-else assignment. It is useful when you want to choose between two values, but it should be used carefully. If the expression becomes too complex, a normal if statement is usually better.

In general:


if (condition)
{
    // use this for control flow
}
Enter fullscreen mode Exit fullscreen mode

And:

auto value = condition ? value_if_true : value_if_false;
Enter fullscreen mode Exit fullscreen mode

Use the ternary operator mainly for simple value selection.

Readable code is more important than short code.

Top comments (0)