capypad
0 day streak
cpp / beginner
Snippet

Boolean Variables and Logical Operators

Boolean (bool) stores true (1) or false (0). Logical operators include && (AND), || (OR), and ! (NOT). The boolalpha manipulator makes cout display true/false instead of 1/0.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
 
int main() {
bool isActive = true;
bool isLoggedIn = false;
cout << boolalpha;
cout << "Active: " << isActive << endl;
cout << "Logged in: " << isLoggedIn << endl;
cout << "Can access: " << (isActive && !isLoggedIn) << endl;
return 0;
}
Breakdown
1
bool isActive = true;
Boolean variable storing true value
2
cout << boolalpha;
Changes output to show true/false instead of 1/0
3
(isActive && !isLoggedIn)
AND operator with NOT on isLoggedIn