Notifications
Clear all
Topic starter 01/08/2025 11:51 pm
In C#, Boolean statements are all about working with true
and false
—perfect for controlling program flow and making decisions.
🔢 Boolean Data Type
- Declared using
bool
- Can only hold
true
orfalse
bool isOnline = true;
bool hasAccess = false;
🧪 Using Boolean Conditions
Often inside control statements like if
, while
, or for
:
if (isOnline)
{
Console.WriteLine("User is online");
}
You can also use Boolean expressions:
int age = 20;
bool isAdult = (age >= 18); // Evaluates to true
⚙️ Logical Operators with Booleans
Combine or manipulate Boolean values:
Operator | Meaning | Example | ||||
---|---|---|---|---|---|---|
&& |
AND | isOnline && hasAccess |
||||
` | ` | OR | `isOnline | hasAccess` | ||
! |
NOT | !isOnline |
||||
== |
Equal | isOnline == true |
||||
!= |
Not Equal | isOnline != false |
✨ Ternary Operator (Short Conditional)
string status = isOnline ? "Online" : "Offline";
🔄 Sample Use Case
bool isLoggedIn = true;
bool isAdmin = false;
if (isLoggedIn && isAdmin)
{
Console.WriteLine("Access granted.");
}
else
{
Console.WriteLine("Access denied.");
}