if...else is used when you need to check whether a condition is true or false and run a different code path accordingly. You'll run into this decision logic constantly — checking login access, whether a mark is a pass, whether stock is available, and so on.
csharp
int mark = 75;
if (mark >= 80)
{
Console.WriteLine("Distinction");
}
else if (mark >= 40)
{
Console.WriteLine("Pass");
}
else
{
Console.WriteLine("Fail");
}Condition flow
- First it checks
mark >= 80. If that's false, it moves on to the next condition. mark >= 40is true, so it printsPassas the output.- Only when none of the conditions above are true does it fall into the
elseblock.
You should see
PassInfo
💡 Practical use
You'll reach for if...else all the time — checking user roles, payment status, whether form input is valid, and more.