Loading...

Decision Making in C#: If, Else and Switch

Imagine you’re a teacher grading exams. If a student scores above 90, you give an A. If they score above 80, you give a B. If they fail, you give an F.

These are decisions, simple, everyday decisions.

Computers also make decisions, but they follow strict rules.
Today, you’ll teach C# how to choose different actions based on different conditions, just like a real teacher (but much faster!).

By the end of this lesson, you’ll build a complete Grading System App.

The Project: Grading System App

Your program will ask the user for a score and print a grade:

text
5 lines
|
23/ 500 tokens
1
2
3
4
5
========================
   GRADING SYSTEM
   Score: 87
   Grade: B
========================

Let’s learn the tools needed to make this work. Conditional logic allows your program to run different code depending on what is true.

C# offers several ways to do this:

  • if
  • else if
  • else
  • switch

These are the brain of your program, the part that thinks.

The If Statement

The simplest form:

csharp
4 lines
|
12/ 500 tokens
1
2
3
4
if (score > 90)
{
    Console.WriteLine("A");
}
Code Tools

This only runs if the condition is true.

If–Else Chain

Use this when you have multiple conditions:

csharp
12 lines
|
35/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
if (score >= 90)
{
    Console.WriteLine("A");
}
else if (score >= 80)
{
    Console.WriteLine("B");
}
else
{
    Console.WriteLine("F");
}
Code Tools

Only one of these blocks will run.

Nested Conditions

You can put one if inside another to create more detailed checks:

csharp
5 lines
|
21/ 500 tokens
1
2
3
4
5
if (score >= 60)
{
    if (score >= 90)
        Console.WriteLine("Excellent!");
}
Code Tools

But use nesting only when needed, too much makes code harder to read.

Switch Statement

A switch checks one value and chooses from many cases.

Example:

csharp
14 lines
|
57/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch (option)
{
    case 1:
        Console.WriteLine("Start game");
        break;

    case 2:
        Console.WriteLine("Settings");
        break;

    default:
        Console.WriteLine("Invalid choice");
        break;
}
Code Tools

Great for designing menus and category-based decisions.

Let’s Build the Grading System App

Here is the complete program:

csharp
29 lines
|
177/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter your score:");
        int score = Convert.ToInt32(Console.ReadLine());

        string grade;

        if (score >= 90)
            grade = "A";
        else if (score >= 80)
            grade = "B";
        else if (score >= 70)
            grade = "C";
        else if (score >= 60)
            grade = "D";
        else
            grade = "F";

        Console.WriteLine("========================");
        Console.WriteLine("   GRADING SYSTEM");
        Console.WriteLine($"   Score: {score}");
        Console.WriteLine($"   Grade: {grade}");
        Console.WriteLine("========================");
    }
}
Code Tools

This program makes a decision every time based on user input.

How It Works

  • User enters a score.
  • Program checks each condition in order.
  • As soon as one condition is true, the matching grade is assigned.
  • Output is printed as a formatted card.

Test it in the DevsCall AI Online Runner to see how it responds to different scores.

Learn Together with AI

Try these prompts with your AI Copilot:

  • “Rewrite the grading system using switch instead of if–else.”
  • “Add + and – grades (A+, B-, etc.).”
  • “Explain nested conditions with real examples.”
  • “Make a version that handles invalid input safely.”

AI will help you expand your logic step-by-step.

Practice Time

Create a program that:

  • Asks the user to choose an operation:
    1. Add
    2. Subtract
    3. Multiply
    4. Divide
  • Then uses switch to perform the correct action.

Expected menu:

text
7 lines
|
27/ 500 tokens
1
2
3
4
5
6
7
========================
   SIMPLE MENU
1. Add
2. Subtract
3. Multiply
4. Divide
========================

Use switch to handle user choices cleanly.

Example Solution

csharp
37 lines
|
258/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("========================");
        Console.WriteLine("   SIMPLE MENU");
        Console.WriteLine("1. Add");
        Console.WriteLine("2. Subtract");
        Console.WriteLine("3. Multiply");
        Console.WriteLine("4. Divide");
        Console.WriteLine("========================");
        Console.WriteLine("Choose an option:");

        int option = Convert.ToInt32(Console.ReadLine());

        switch (option)
        {
            case 1:
                Console.WriteLine("You chose Add.");
                break;
            case 2:
                Console.WriteLine("You chose Subtract.");
                break;
            case 3:
                Console.WriteLine("You chose Multiply.");
                break;
            case 4:
                Console.WriteLine("You chose Divide.");
                break;
            default:
                Console.WriteLine("Invalid choice.");
                break;
        }
    }
}
Code Tools

If your version behaves the same, congratulations, your program can think!