C++ Booleans

πŸ”˜ C++ Booleans

C++ mein Boolean (bool) data type ka use true / false values store karne ke liye hota hai.
Ye conditions, decision making, loops ka base hai.


πŸ”Ή 1. Boolean Declaration

bool isActive = true;
bool isLoggedIn = false;
  • true β†’ 1

  • false β†’ 0


πŸ”Ή 2. Boolean Output

bool result = true;
cout << result;

Output:

1

πŸ”Ή 3. Print true / false as Text

#include <iostream>
using namespace std;
int main() {
bool status = false;
cout << boolalpha << status;
return 0;
}

Output:

false

πŸ”Ή 4. Boolean from Comparison

int a = 10, b = 5;
bool check = (a > b);
cout << check; // 1

πŸ”Ή 5. Boolean in if Statement

bool isAdult = true;

if (isAdult) {
cout << “Access Granted”;
} else {
cout << “Access Denied”;
}


πŸ”Ή 6. Boolean with Logical Operators

bool x = true, y = false;

cout << (x && y); // AND β†’ 0
cout << (x || y); // OR β†’ 1
cout << (!x); // NOT β†’ 0


πŸ”Ή 7. Boolean User Input

bool flag;
cin >> flag;

πŸ‘‰ User 1 (true) ya 0 (false) enter karta hai.


πŸ”Ή 8. Boolean Size

cout << sizeof(bool);

➑ Usually 1 byte


❌ Common Mistakes

bool x = "true"; // ❌ wrong

βœ” Correct:

bool x = true;

πŸ“Œ Summary

  • bool sirf true/false store karta hai

  • Default output = 1 / 0

  • boolalpha se readable output

  • Conditions & loops ke liye essential

You may also like...