Lesson 16
Boolean
Covers bool, expressions, logical operators, boolalpha.
Lesson content
What is boolean in C++?
Bool stores only true or false.
bool isLoggedIn = true;
bool hasLicense = false;Why does boolean exist?
- Is the password correct?
- Is the user an admin?
- Has payment completed?
- Is student eligible for admission?
Bool beats raw 1/0 for readability.
Real-world use cases
- Login and authentication systems
- Banking applications
- Online shopping websites
- Game development
- Attendance systems
- Traffic light simulations
- Access control systems
- Form validation
Boolean data type
bool variableName;
bool isStudent = true;Boolean values
Value | Meaning
----- | -------------------
true | Condition is true
false | Condition is falsebool isPassed = true;
bool isMember = false;Boolean expressions
Expression evaluating to true or false.
age >= 18
marks >= 50
number == 100Relational operators
Operator | Meaning | Example
-------- | ------------------------ | --------
== | Equal to | a == b
!= | Not equal to | a != b
> | Greater than | a > b
< | Less than | a < b
>= | Greater than or equal to | a >= b
<= | Less than or equal to | a <= bLogical operators
&& Logical AND
|| Logical OR
! Logical NOTage >= 18 && hasLicense
marks >= 50 || sportsQuota
!isLoggedInBoolean output
C++ prints bools as numbers by default.
true -> 1
false -> 0Use boolalpha for word output.
cout << boolalpha << true;
// Output: trueExample 1: Boolean variable
#include <iostream>
using namespace std;
int main()
{
bool isStudent = true;
cout << boolalpha;
cout << isStudent;
return 0;
}Example 2: Boolean expression
#include <iostream>
using namespace std;
int main()
{
int age = 20;
bool canVote = age >= 18;
cout << boolalpha;
cout << canVote;
return 0;
}Example 3: Boolean with if
#include <iostream>
using namespace std;
int main()
{
bool isLoggedIn = true;
if (isLoggedIn)
{
cout << "Welcome!";
}
else
{
cout << "Please log in.";
}
return 0;
}Example 4: Logical AND
#include <iostream>
using namespace std;
int main()
{
int age = 22;
bool hasLicense = true;
if (age >= 18 && hasLicense)
{
cout << "You can drive.";
}
else
{
cout << "You cannot drive.";
}
return 0;
}Example 5: Password verification
#include <iostream>
#include <string>
using namespace std;
int main()
{
string password;
cout << "Enter password: ";
cin >> password;
bool isCorrect = (password == "admin123");
if (isCorrect)
{
cout << "Access Granted";
}
else
{
cout << "Access Denied";
}
return 0;
}Common mistakes and pitfalls
- = instead of ==. Fix: use == to compare
- Comparing bool to true. Fix: just use if(isReady)
- Forgetting boolalpha. Fix: add it before output
- Numbers instead of true/false. Fix: use bool literals
- Confusing && and ||. Fix: pick correct operator
// Wrong
if (isLoggedIn == true)
{
cout << "Welcome";
}// Correct
if (isLoggedIn)
{
cout << "Welcome";
}Best practices
- Use clear names like isValid
- Prefer true/false over 1/0
- Use boolalpha for readable output
- Keep boolean expressions simple
- Combine conditions only when needed
When not to use this
- More than two states needed. Use enum
- Numerical calculations required
- Multiple related values. Use structs or classes
Summary
- Boolean uses the bool data type
- Stores only true or false
- Expressions evaluate to true or false
- Relational operators produce booleans
- Logical operators combine booleans
- Default output is 1 or 0
- boolalpha prints true and false
- Use clear names for readability
Frequently asked questions
What is a boolean in C++?
Data type storing true or false.
Why does true print as 1?
Default is numeric. Use boolalpha for words.
What is the difference between bool and int?
Bool holds true/false. Int holds whole numbers.
What are boolean expressions?
Expressions giving true or false, like age >= 18.
When should I use a boolean variable?
Use for two-state conditions like yes/no.