C# If … Else

C# If … Else

The if...else statement in C# is used to make decisions. It allows your program to execute different code blocks based on conditions.


 Basic if Statement


 

✔ Executes only when the condition is true.


if...else Statement


 


if...else if...else Ladder

Used when there are multiple conditions.


 


 Nested if Statement


 


 Using Logical Operators


 


 Short if (Ternary Operator)


 


if Without Braces (Not Recommended)

⚠ Avoid this for readability.


 Common Mistakes

❌ Using = instead of ==
❌ Missing braces {}
❌ Incorrect condition logic


 Summary

if checks condition
else runs when condition is false
else if handles multiple conditions
✔ Ternary is shorthand for simple checks

You may also like...