Back to C++
2026-01-055 min read

Boolean Expressions (C++)

Learn Boolean Expressions (C++) step by step with clear examples and exercises.

Why This Matters

Boolean expressions are a fundamental part of C++ programming, allowing you to make decisions and control program flow using logical operators like &&, ||, and !. In this lesson, we'll explore the ins and outs of Boolean expressions in C++, including their importance, prerequisites, core concepts, worked example, common mistakes, practice questions, and frequently asked questions.

Why This Matters

Boolean expressions play a crucial role in programming by enabling you to create conditional statements that determine the flow of your code based on true or false values. Understanding Boolean expressions is essential for writing efficient and effective C++ programs, as well as for solving real-world problems and acing coding interviews.

Prerequisites

To follow this lesson, you should be familiar with the following topics:

  • Basic C++ syntax and syntax rules (variables, data types, operators)
  • Control structures (if-else statements, loops)

Core Concept

What are Boolean expressions?

Boolean expressions are expressions that evaluate to either true or false. In C++, they are used in conditional statements like if, while, and for to make decisions and control program flow. The three logical operators used in Boolean expressions are:

  • && (logical AND): Returns true if both operands are true; otherwise, it returns false.
  • || (logical OR): Returns true if at least one operand is true; otherwise, it returns false.
  • ! (logical NOT): Negates the value of its operand. If the operand is true, ! returns false, and vice versa.

Truth tables for logical operators

To better understand how these operators work, let's look at their truth tables:

| A | B | A && B | A || B | !A |

|---|---|---|---|---|

| T | T | T | T | F |

| T | F | F | T | F |

| F | T | F | T | T |

| F | F | F | F | T |

Short-circuit evaluation

C++ uses short-circuit evaluation for logical operators. This means that if the outcome of an expression can be determined with only one operand, the other operand will not be evaluated. For example:

if (a > 10 && b < 5) {
// code block executed if a > 10 AND b < 5
}

In this case, if a is greater than 10, the expression a > 10 && b < 5 is already known to be true, so the value of b does not need to be checked. This can save time and improve program efficiency.

Precedence and associativity

Logical operators have a higher precedence than arithmetic operators in C++. This means that they are evaluated before arithmetic operators when an expression contains both types of operators. For example:

int x = 5 && 2 + 3; // evaluates to 2, because the logical AND operator is evaluated first

Logical operators are left-associative, which means that they group from left to right when multiple occurrences appear in an expression. For example:

int y = !(a && b) || c; // groups as (!(a && b)) || c

Compound Boolean expressions

Compound Boolean expressions are expressions that combine logical operators with parentheses to form more complex conditions. They can help make your code clearer and easier to understand by grouping related operations together. For example:

if ((a > 10) || (b < 5)) {
// code block executed if either a > 10 OR b < 5
}

Conversion rules for Boolean expressions

In C++, all non-zero values are considered true, while zero and the null pointer are considered false. This means that you can use any numeric or pointer value in a Boolean expression. For example:

if (5) {
// code block executed because 5 is a non-zero value
}

int x = 0;
if (x) {
// code block not executed because x is 0
}

Worked Example

Let's create a simple program that uses Boolean expressions to check if a user is eligible to vote:

#include <iostream>
using namespace std;

int main() {
int age, citizen;

cout << "Enter your age: ";
cin >> age;

cout << "Are you a citizen? (1 for yes, 0 for no): ";
cin >> citizen;

if ((age >= 18) && (citizen == 1)) {
cout << "You are eligible to vote." << endl;
} else {
cout << "You are not eligible to vote." << endl;
}

return 0;
}

In this example, we use the && operator to check if the user's age is greater than or equal to 18 and if they are a citizen (represented by a 1). If both conditions are true, the user is eligible to vote. Otherwise, they are not eligible.

Common Mistakes

  1. Forgetting parentheses: Parentheses help clarify complex Boolean expressions and prevent confusion about which operators should be evaluated first. For example:
if (a > b && c < d) { // correct
cout << "Condition is true." << endl;
}
if (a > b && c < d || e > f) { // incorrect, should be: if ((a > b && c < d) || e > f)
cout << "Condition is true." << endl;
}
  1. Using the wrong logical operator: Be sure to use the correct logical operator for your intended purpose. For example, using || instead of && can lead to unexpected results:
if (a == 5 || b == 10) { // incorrect, should be: if (a == 5 && b == 10)
cout << "Condition is true." << endl;
}
  1. Neglecting short-circuit evaluation: When using logical operators in a conditional statement, remember that C++ uses short-circuit evaluation. This means that if the outcome of an expression can be determined with only one operand, the other operand will not be evaluated:
int x = 0;
if (x > 10 || y < 5) { // incorrect, should be: if (y < 5) because x > 10 is already known to be false
cout << "Condition is true." << endl;
}

Practice Questions

  1. Write a program that checks if a number is even or odd using Boolean expressions.
  2. Modify the voting eligibility program to also check if the user has a valid ID.
  3. Write a program that uses compound Boolean expressions to check if a student's grades meet the requirements for graduation.

FAQ

  1. What happens when I use a non-numeric value in a Boolean expression? In C++, any non-zero value is considered true, while zero and the null pointer are considered false. This means that you can use any numeric or pointer value in a Boolean expression. For example:
if (5) { // code block executed because 5 is a non-zero value
cout << "Condition is true." << endl;
}
int x = 0;
if (x) { // code block not executed because x is 0
cout << "Condition is true." << endl;
}
  1. Can I use the == operator in a Boolean expression? Yes, you can use the == operator to compare two values and include the result in a Boolean expression. For example:
if (a == 5) { // code block executed if a is equal to 5
cout << "Condition is true." << endl;
}
  1. Why are logical operators important in programming? Logical operators are essential for making decisions and controlling program flow using conditional statements like if, while, and for. They allow you to write efficient and effective C++ programs, solve real-world problems, and excel in coding interviews.
Boolean Expressions (C++) | C++ | XQA Learn