Introduction
Booleans help us make decisions in code by representing true or false values.
Booleans help us make decisions in code by representing true or false values.
bool variableName = true; // or false
Boolean variables can only hold true or false.
Use booleans to control program flow with if statements.
isRaining and sets it to true.bool isRaining = true;hasFinished and sets it to false.bool hasFinished = false;isAdult to true if age is 18 or more, otherwise false.bool isAdult = age >= 18;This program shows how a boolean variable isDoorOpen controls the message printed. It starts false, then changes to true, and the if statement checks it.
using System; class Program { static void Main() { bool isDoorOpen = false; Console.WriteLine("Is the door open? " + isDoorOpen); isDoorOpen = true; if (isDoorOpen) { Console.WriteLine("You can enter the room."); } else { Console.WriteLine("The door is closed."); } } }
Boolean values are case-sensitive: use true and false in lowercase.
Booleans are often used in if, while, and for statements to control flow.
Booleans store only two values: true or false.
They help your program make decisions.
Use them with conditions to control what your program does.
true or false.true is a valid Boolean. Numbers or words like yes are not Booleans.bool (all lowercase).bool isActive = true; uses bool and assigns true correctly. boolean isActive = true; uses boolean which is incorrect in C#. Bool isActive = true; uses Bool with uppercase B, which is invalid. bool isActive = "true"; assigns a string "true" instead of Boolean true.bool a = true;
bool b = false;
Console.WriteLine(a && b);
a is true and b is false. true AND false is false, which Console.WriteLine outputs as False.bool isReady = True;
if(isReady)
{
Console.WriteLine("Ready!");
}
true and false. True with uppercase T is invalid.Console.WriteLine syntax is correct. Variable name 'isReady' is valid.bool isLoggedIn = true;
bool isAdmin = false;
// Which condition to use in if?
&& ensures both conditions are true. OR || would allow access if either is true, which is incorrect.Pass the test and this pattern moves unknown