Boolean
Part of the Fundamentals section of Coddy's C++ journey — lesson 8 of 74.
A boolean type has only 2 possible values: true or false.
To assign a boolean value to a variable, use the keyword bool followed by the variable name:
bool variable_true = true;
bool variable_false = false;In the above example, two boolean variables named variable_true and variable_false are initialized with the values true and false, respectively. When printing a boolean value using cout, true displays as 1 and false displays as 0.
Booleans are the building blocks for creating logic in the programs we write. We have a whole chapter about logic and conditions.
Challenge
BeginnerDeclare a variable named isLoggedIn and assign it the value true.
Cheat sheet
A boolean type has only 2 possible values: true or false.
To declare a boolean variable, use the keyword bool:
bool variable_true = true;
bool variable_false = false;When printing boolean values with cout, true displays as 1 and false displays as 0.
Try it yourself
#include <iostream>
int main() {
// Type your code below
bool isLoggedIn = ?
// Don\'t change the line below
std::cout << "isLoggedIn = " << isLoggedIn;
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison3Variables Part 2
Type DeclarationNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementConditional OperatorRecap - If ElseNested If - Else