Forum

Learn C# Boolean St…
 
Notifications
Clear all

Learn C# Boolean Statements

1 Posts
1 Users
0 Reactions
12 Views
 josh
(@josh)
Member Admin
Joined: 2 months ago
Posts: 510
Topic starter  

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 or false
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.");
}

 


   
Quote
Share: