C# Basics

Conditions and Switch Expressions

Choose behavior with if statements and concise switch expressions.

Introduction

Switch expressions map an input to a result and require every relevant case to be handled.

Relational patterns make numeric ranges readable. The final discard pattern handles every remaining score.

See how it works

Move the slider to change the score. The flowchart lights up the path the program takes, and the matching line of code is highlighted.

Runnable C# example

C#
Run code →
Program.cs
using System;

class Program
{
    static void Main()
    {
        int score = 87;
        string grade = score switch
        {
            >= 90 => "A",
            >= 80 => "B",
            >= 70 => "C",
            _ => "Needs practice"
        };
        Console.WriteLine(grade);
    }
}
Result of running the code
B

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges