Lesson 15 +10 XP

C# Booleans

A bool data type stores one of two values: true or false.

Declaring a boolean


csharp
bool isCSharpFun = true;
bool isFishTasty = false;

### Console.WriteLine(isCSharpFun);   // True

### Console.WriteLine(isFishTasty);   // False

Boolean values from expressions

Comparison operators produce booleans:


csharp
int x = 10;
int y = 9;
Console.WriteLine(x > y);   // True, because 10 is greater than 9

### Console.WriteLine(10 > 9);    // True

### Console.WriteLine(10 == 15);  // False

Booleans in conditions

Booleans are the heart of decisions. Conditions like if check a boolean:


csharp
if (age >= 18)
{

### Console.WriteLine("Adult");
}

If the boolean is true, the code runs; if false, it doesn't.

TL;DR

  • bool holds true or false.
  • Comparisons like x > y produce booleans.
  • if statements run code based on a boolean.