C++ Boolean Expressions

πŸ”˜ C++ Boolean Expressions

Boolean Expression wo expression hota hai jiska result sirf true ya false hota hai.
Ye mostly conditions, if-else, loops, aur decision making mein use hota hai.


πŸ”Ή 1. Simple Boolean Expression

10 > 5

βœ” Result: true

5 == 10

βœ” Result: false


πŸ”Ή 2. Boolean Expression with Variables

int a = 10, b = 20;

bool result = (a < b);
cout << result; // 1


πŸ”Ή 3. Using Comparison Operators

int x = 5;

x == 5 // true
x != 3 // true
x > 10 // false
x <= 5 // true


πŸ”Ή 4. Boolean Expression in if Statement

int age = 18;

if (age >= 18) {
cout << "Eligible to vote";
}


πŸ”Ή 5. Boolean Expression with Logical Operators

int age = 25;
bool hasID = true;

if (age >= 18 && hasID) {
cout << "Entry allowed";
}


πŸ”Ή 6. Complex Boolean Expression

int a = 10, b = 5, c = 8;

bool result = (a > b && b < c) || (c == 8);
cout << result;


πŸ”Ή 7. Boolean Expression in Loops

While Loop

int i = 1;

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

For Loop

for (int i = 0; i < 5; i++) {
cout << i << " ";
}

πŸ”Ή 8. Using boolalpha

cout << boolalpha << (10 > 20);

Output:

false

πŸ”Ή 9. Function Returning Boolean

bool isEven(int x) {
return x % 2 == 0;
}

❌ Common Mistakes

if (x = 5) // ❌ assignment

βœ” Correct:

if (x == 5)

πŸ“Œ Summary

  • Boolean expression result = true / false

  • Comparison + Logical operators se banta hai

  • if, while, for mein use hota hai

  • Clear logic ke liye parentheses use karein

You may also like...