Booleans Real-Life Examples
Booleans in Decision Making
Booleans represent true/false, yes/no, or on/off conditions. They control program flow by enabling or disabling actions.
Think of them like switches: 0 = false (off), 1 = true (on).
Examples in Everyday Programs
• **Login system**: `bool isAuthenticated = true;`
• **Shopping cart**: `bool hasItems = (cartCount > 0);`
• **Game**: `bool gameOver = false;`
• **Network app**: `bool connectionAlive = ping();`
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool loggedIn = true;
bool hasPremium = false;
if (loggedIn && hasPremium)
printf("Welcome premium user!\n");
else if (loggedIn)
printf("Welcome regular user!\n");
else
printf("Please log in.\n");
return 0;
}
Welcome regular user!
Boolean Flags in Systems
Flags are often represented by Booleans: e.g., `isRunning`, `isVisible`, `isCompleted`.
They simplify readability and reduce bugs compared to using raw integers or magic values.
Best Practices
• Use descriptive names like `isReady`, `hasError`, `canProceed`.
• Keep Booleans simple: avoid encoding extra states with integers.
• Group related flags into structs if many are used together.