Explain it simply
If...Else, in plain language
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.
Problem → Why → Solution
The problem — why this concept matters
Putting a broad condition first or missing one side of a boundary creates unreachable or misclassified branches.
A common mistake
Assuming one successful happy path proves the code is correct, maintainable, and production-ready.
Core concept — understand how it works
An `if` selects a branch from a Boolean condition. Clarify domain rules with named booleans or methods, and verify ranges, null guards, branch order, and boundaries.
- typed input
- C# expression and method
- CLR value or object state
Explain the core concept and runtime behavior of If...Else
Core syntax or pattern
int mark = 75;
if (mark >= 80)
{
Console.WriteLine("Distinction");
}
else if (mark >= 40)
{
Console.WriteLine("Pass");
}
else
{
Console.WriteLine("Fail");
}An `if` selects a branch from a Boolean condition. Clarify domain rules with named booleans or methods, and verify ranges, null guards, branch order, and boundaries.
Read the code step by step
- typed input
int mark = 75;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.
- C# expression and method
{An `if` selects a branch from a Boolean condition. Clarify domain rules with named booleans or methods, and verify ranges, null guards, branch order, and boundaries.
- CLR value or object state
}Test a score-grading rule with exact boundaries 59, 60, 79, and 80 plus an invalid range.
- typed input
- C# expression and method
- CLR value or object state
Success
Explain the core concept and runtime behavior of If...Else
Failure
Putting a broad condition first or missing one side of a boundary creates unreachable or misclassified branches.
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.
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.
PassInfo
💡 Practical use
You'll reach for if...else all the time — checking user roles, payment status, whether form input is valid, and more.
Real-world usage
How to apply it in a real project
Test a score-grading rule with exact boundaries 59, 60, 79, and 80 plus an invalid range.
Mini challenge
Build it yourself
Test a score-grading rule with exact boundaries 59, 60, 79, and 80 plus an invalid range.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
int mark = 75;
if (mark >= 80)
{
Console.WriteLine("Distinction");
}
else if (mark >= 40)
{
Console.WriteLine("Pass");
}
else
{
Console.WriteLine("Fail");
}Expected: Explain the core concept and runtime behavior of If...Else
Show hint
Test a score-grading rule with exact boundaries 59, 60, 79, and 80 plus an invalid range.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- typed input → C# expression and method → CLR value or object state
- An `if` selects a branch from a Boolean condition. Clarify domain rules with named booleans or methods, and verify ranges, null guards, branch order, and boundaries.
- Putting a broad condition first or missing one side of a boundary creates unreachable or misclassified branches.
- Test a score-grading rule with exact boundaries 59, 60, 79, and 80 plus an invalid range.
C# documentation — Microsoft Learn