C# Booleans

Understanding true and false values

✅ What are Booleans?

Booleans represent true or false values in C#. They are essential for making decisions in your code and controlling program flow based on conditions.


// Creating boolean variables
bool isActive = true;
bool isComplete = false;
Console.WriteLine($"Active: {isActive}, Complete: {isComplete}");
                                    

Output:

Active: True, Complete: False

Boolean Basics

✔️

True Value

Represents a positive condition

bool isReady = true;
Console.WriteLine(isReady);

False Value

Represents a negative condition

bool isLocked = false;
Console.WriteLine(isLocked);
🔍

Comparisons

Compare values to get booleans

int age = 18;
bool isAdult = age >= 18;
🔗

Logical Operators

Combine boolean expressions

bool a = true, b = false;
bool result = a && b;

🔹 Boolean Values

The bool data type in C# represents a Boolean logical value, which can only be true or false. These literals are reserved keywords and must be written in lowercase. Boolean variables are fundamental to control flow, enabling decision-making through conditional statements like if, while, and for. They are also the result of comparison and logical operations. Understanding Boolean logic is essential for writing correct conditions and predicates, especially in LINQ queries, validation logic, and state machines. The .NET Framework also provides the Nullable<bool> (or bool?) type to represent a third, "undefined" state when needed.

// Declaring boolean variables
bool isRaining = true;
bool isSunny = false;

Console.WriteLine($"Is it raining? {isRaining}");
Console.WriteLine($"Is it sunny? {isSunny}");

// Booleans in conditions
if (isRaining)
{
    Console.WriteLine("Take an umbrella!");
}

Output:

Is it raining? True

Is it sunny? False

Take an umbrella!

🔹 Comparison Operators

Comparison operators like ==, !=, >, <, >=, and <= compare values and return booleans (true/false). They drive decision-making in if statements and loops. For example, 10 > 5 returns true, directing program flow. Essential for validation, filtering, and logic gates, these operators underpin user authentication, data sorting, and interactive features, ensuring programs respond correctly to varying conditions and inputs.

int x = 10;
int y = 20;

// Equal to
bool isEqual = (x == y);
Console.WriteLine($"{x} == {y}: {isEqual}");

// Not equal to
bool notEqual = (x != y);
Console.WriteLine($"{x} != {y}: {notEqual}");

// Greater than
bool greater = (x > y);
Console.WriteLine($"{x} > {y}: {greater}");

// Less than or equal to
bool lessOrEqual = (x <= y);
Console.WriteLine($"{x} <= {y}: {lessOrEqual}");

Output:

10 == 20: False

10 != 20: True

10 > 20: False

10 <= 20: True

🔹 Logical Operators

Logical operators—AND (&&), OR (||), and NOT (!)—combine boolean conditions for complex decision logic. AND requires all conditions true; OR needs at least one true; NOT inverts a value. For instance, age > 18 && hasLicense checks driving eligibility. These operators enable sophisticated control flows in algorithms, user permissions, and system checks, forming the backbone of reliable, multi-condition evaluations in software development.

🔸 AND Operator (&&)

bool hasLicense = true;
bool hasInsurance = true;

bool canDrive = hasLicense && hasInsurance;
Console.WriteLine($"Can drive: {canDrive}");

Output:

Can drive: True

🔸 OR Operator (||)

bool isWeekend = false;
bool isHoliday = true;

bool canRelax = isWeekend || isHoliday;
Console.WriteLine($"Can relax: {canRelax}");

Output:

Can relax: True

🔸 NOT Operator (!)

bool isRaining = false;
bool isSunny = !isRaining;

Console.WriteLine($"Is sunny: {isSunny}");

Output:

Is sunny: True

🔹 Boolean Expressions

Boolean expressions evaluate to either true or false and are foundational for program control flow. They combine variables, values, and operators to form conditions used in if statements and loops. For instance, (score >= 50) yields true if the score is 50 or higher, directing execution accordingly.

int age = 25;
bool hasID = true;

// Complex boolean expression
bool canEnterClub = (age >= 21) && hasID;
Console.WriteLine($"Can enter club: {canEnterClub}");

// Multiple conditions
int score = 85;
bool isPassing = (score >= 60) && (score <= 100);
Console.WriteLine($"Is passing: {isPassing}");

Output:

Can enter club: True

Is passing: True

🔹 Practical Boolean Examples

Booleans are used in real-world scenarios like user authentication, access control, and system checks. For example, a login system returns true if credentials match, granting access. Age verification for voting or driving uses boolean logic to enforce rules, such as canVote = (age >= 18).

// User authentication
string username = "admin";
string password = "1234";
bool isLoggedIn = (username == "admin") && (password == "1234");
Console.WriteLine($"Login successful: {isLoggedIn}");

// Age verification
int userAge = 16;
bool canVote = userAge >= 18;
bool canDrive = userAge >= 16;
Console.WriteLine($"Can vote: {canVote}");
Console.WriteLine($"Can drive: {canDrive}");

// Temperature check
int temperature = 75;
bool isComfortable = (temperature >= 65) && (temperature <= 80);
Console.WriteLine($"Temperature is comfortable: {isComfortable}");

Output:

Login successful: True

Can vote: False

Can drive: True

Temperature is comfortable: True

🧠 Test Your Knowledge

What does the expression (5 > 3) && (2 < 4) evaluate to?