codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

11 / 87 topics
6Keywords and Identifiers7Variables, Literals, Constants & Storage Classes8Data Types & Type Modifiers9Type Conversion & Casting Operators10Operators11Booleans12Math & Numbers13Date and Time
Tutorials/C++ Programming/Booleans
⚡C++ Programming

Booleans

Updated 2026-05-12
15 min read

Booleans

In this tutorial, we'll explore the fundamental concept of booleans in C++. Understanding booleans is crucial as they form the basis for decision-making processes in programming. Whether you're creating simple conditional statements or complex algorithms, knowing how to work with booleans will greatly enhance your ability to write effective and efficient code.

Introduction

Booleans are a data type that can hold one of two values: true or false. They are named after the mathematician George Boole and are essential for controlling the flow of execution in programs. In C++, the bool keyword is used to declare boolean variables.

The bool Type

In C++, the bool type is used to store boolean values (true or false). Here's how you can declare and initialize a boolean variable:

bool_example.cpp
1#include <iostream>
2
3int main() {
4 bool isTrue = true;
5 bool isFalse = false;
6
7 std::cout << "isTrue: " << isTrue << std::endl;
8 std::cout << "isFalse: " << isFalse << std::endl;
9
10 return 0;
11}
Output
isTrue: 1
isFalse: 0

Explanation:

  • The bool keyword is used to declare a boolean variable.
  • The values true and false are the only valid literals for boolean variables in C++.
  • When outputting a boolean value, true is represented as 1 and false as 0.

Boolean Expressions

A boolean expression is an expression that evaluates to either true or false. These expressions often involve comparison operators (==, !=, <, >, <=, >=) and logical operators (&&, ||, !). Here are some examples:

bool_expressions.cpp
1#include <iostream>
2
3int main() {
4 int a = 10;
5 int b = 20;
6
7 bool isEqual = (a == b);
8 bool isNotEqual = (a != b);
9 bool isGreater = (a > b);
10
11 std::cout << "isEqual: " << isEqual << std::endl;
12 std::cout << "isNotEqual: " << isNotEqual << std::endl;
13 std::cout << "isGreater: " << isGreater << std::endl;
14
15 return 0;
16}
Output
isEqual: 0
isNotEqual: 1
isGreater: 0

Explanation:

  • isEqual evaluates to false because a is not equal to b.
  • isNotEqual evaluates to true because a is indeed not equal to b.
  • isGreater evaluates to false because a is less than b.

Truthy/Falsy in C++

In C++, the concept of truthy and falsy values doesn't exist like it does in some other languages (e.g., JavaScript). However, when a boolean context is required, non-zero integers are considered true, while zero is considered false. This behavior can be observed when using conditional statements.

truthy_falsy.cpp
1#include <iostream>
2
3int main() {
4 int value = 5;
5 if (value) {
6 std::cout << "Value is truthy." << std::endl;
7 } else {
8 std::cout << "Value is falsy." << std::endl;
9 }
10
11 return 0;
12}
Output
Value is truthy.

Explanation:

  • Since value is a non-zero integer, it evaluates to true in the boolean context of the if statement.

Boolean with if Statements

Conditional statements like if, else, and else if are used to execute different blocks of code based on the evaluation of boolean expressions. Here's a simple example:

if_statement.cpp
1#include <iostream>
2
3int main() {
4 int age = 20;
5
6 if (age >= 18) {
7 std::cout << "You are an adult." << std::endl;
8 } else {
9 std::cout << "You are a minor." << std::endl;
10 }
11
12 return 0;
13}
Output
You are an adult.

Explanation:

  • The if statement checks if the condition (age >= 18) is true.
  • If the condition is true, it executes the code block under the if label.
  • Otherwise, it executes the code block under the else label.

Practical Example

Let's create a simple program that checks whether a number is positive, negative, or zero:

practical_example.cpp
1#include <iostream>
2
3int main() {
4 int number;
5
6 std::cout << "Enter an integer: ";
7 std::cin >> number;
8
9 if (number > 0) {
10 std::cout << "The number is positive." << std::endl;
11 } else if (number < 0) {
12 std::cout << "The number is negative." << std::endl;
13 } else {
14 std::cout << "The number is zero." << std::endl;
15 }
16
17 return 0;
18}

Explanation:

  • The program prompts the user to enter an integer.
  • It then uses nested if statements to determine if the number is positive, negative, or zero and prints the corresponding message.

Summary

ConceptDescription
bool typeA data type that can hold either true or false.
true/falseThe two possible values of a boolean variable.
Boolean expressionsExpressions that evaluate to true or false, often using comparison and logical operators.
Truthy/FalsyIn C++, non-zero integers are considered true, while zero is considered false.
if statementsConditional statements used to execute code based on boolean evaluations.

What's Next?

Now that you have a solid understanding of booleans, the next step is to explore mathematical operations and number handling in C++. In the Math & Numbers tutorial, we'll dive into various arithmetic operators, data types for numbers, and how to perform complex calculations. This will build on your foundational knowledge and prepare you for more advanced programming concepts.

Stay tuned!


PreviousOperatorsNext Math & Numbers

Recommended Gear

OperatorsMath & Numbers