Thuta Learning
C#
BasicProgrammingBeginner

If...Else

C#Lesson 920 min

What you'll walk away with

  • Explain the core concept and runtime behavior of If...Else
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

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.

Mental model
  1. typed input
  2. C# expression and method
  3. CLR value or object state

Explain the core concept and runtime behavior of If...Else

Core syntax or pattern

csharp
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

  1. typed inputint 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.

  2. 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.

  3. CLR value or object state}

    Test a score-grading rule with exact boundaries 59, 60, 79, and 80 plus an invalid range.

Execution flow
  1. typed input
  2. C# expression and method
  3. 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.

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 >= 40 is true, so it prints Pass as the output.
  • Only when none of the conditions above are true does it fall into the else block.
You should see
Pass

Info

💡 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
csharp
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

What best describes the core idea of If...Else?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Putting a broad condition first or missing one side of a boundary creates unreachable or misclassified branches.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Test a score-grading rule with exact boundaries 59, 60, 79, and 80 plus an invalid range.

You'll know it worked when: Pass

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

If...Else | Thuta Learning