C++ Boolean Examples

πŸ”˜ C++ Boolean Examples

Neeche C++ Boolean (bool) ke simple se advanced examples diye gaye hain, jo concept ko clearly samjhaate hain.


πŸ”Ή 1. Basic Boolean Example

bool isOnline = true;
bool isPaid = false;

cout << isOnline << endl; // 1
cout << isPaid; // 0


πŸ”Ή 2. Boolean with boolalpha

bool status = true;
cout << boolalpha << status;

Output:

true

πŸ”Ή 3. Boolean from Comparison

int a = 10, b = 20;
bool result = (a < b);

cout << result; // 1


πŸ”Ή 4. Boolean in if-else

int age = 16;

if (age >= 18) {
cout << "Adult";
} else {
cout << "Minor";
}


πŸ”Ή 5. Boolean with Logical Operators

bool hasID = true;
bool hasTicket = false;

if (hasID && hasTicket) {
cout << "Entry Allowed";
} else {
cout << "Entry Denied";
}


πŸ”Ή 6. Boolean in Loops

int i = 1;

while (i <= 5) { // boolean expression
cout << i << " ";
i++;
}


πŸ”Ή 7. Function Returning Boolean

bool isPositive(int x) {
return x > 0;
}
cout << isPositive(5); // 1

πŸ”Ή 8. Boolean User Input

bool flag;
cin >> flag;

πŸ‘‰ User input:

  • 1 β†’ true

  • 0 β†’ false


πŸ”Ή 9. Complex Boolean Example

int marks = 75;
bool passed = (marks >= 40);
bool distinction = (marks >= 75);

if (passed && distinction) {
cout << "Distinction Pass";
}


πŸ”Ή 10. Boolean with Ternary Operator

int x = 10;
bool isEven = (x % 2 == 0) ? true : false;

cout << isEven;


❌ Common Mistakes

if (x = 10) // ❌ assignment instead of comparison

βœ” Correct:

if (x == 10)

πŸ“Œ Summary

  • Boolean stores true / false

  • Default output = 1 / 0

  • Used in conditions & loops

  • Logical + comparison operators se banta hai

You may also like...