C# Booleans

C# Booleans

In C#, booleans are used to store true or false values. They are mainly used in conditions, comparisons, and decision-making statements.


 Boolean Data Type

The boolean data type in C# is called bool.



 Boolean Output


Output:

True

 Boolean Expressions

Boolean values are often the result of comparisons.


 


 Comparison Operators with Booleans



 Logical Operators

OperatorDescriptionExample
&&Logical ANDx > 5 && x < 20
``
!Logical NOT!isActive

 


 Booleans in if Statement


 


 Boolean with User Input


 

✔ Safer approach:



 Boolean in Loops


 


 Default Boolean Value


Default value: false


 Summary

bool stores true or false
✔ Used in conditions and decisions
✔ Works with comparison & logical operators
✔ Essential for if, while, and loops

You may also like...