DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 24 of 64
←PreviousPrevNextNext→

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();`

Example
#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;
}
Output
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.

Test your knowledge: Booleans Real-Life Examples
Quiz Configuration
4 of 8 questions
Random
Previous allowed
Review enabled
Early close allowed
Estimated time: 8 min
C BasicsTopic 24 of 64
←PreviousPrevNextNext→