Loading lessons...
C# Booleans
C# Booleans
A bool data type stores one of two values: true or false.
Declaring a boolean
bool isCSharpFun = true;
bool isFishTasty = false;
Console.WriteLine(isCSharpFun); // True
Console.WriteLine(isFishTasty); // False
Boolean values from expressions
Comparison operators produce booleans:
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:
if (age >= 18)
{
Console.WriteLine("Adult");
}
If the boolean is true, the code runs; if false, it doesn't.
TL;DR
boolholdstrueorfalse.- Comparisons like
x > yproduce booleans. ifstatements run code based on a boolean.