×

c# · level 2 · unit 4 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A checks one value and jumps to its .

A long chain of else if checks the same value again and again. A switch checks it once and jumps to the case label that matches. Each case runs its lines and ends with break, which leaves the switch.

Stack two labels with nothing between them and they share one body, as Medium and Large do here. The label catches every value no case named. A switch works on enums, numbers and text.

C# never lets one case run on into the next. Leave out a break and it refuses with CS0163, or CS8070 on the last label.

using System;

Size size = Size.Large;
switch (size)
{
    case Size.Small:
        Console.WriteLine("That is 2.50");
        break;
    case Size.Medium:
    case Size.Large:
        Console.WriteLine("That is 3.20");
        break;
    default:
        Console.WriteLine("Sorry, we do not make that size");
        break;
}

enum Size { Small, Medium, Large, Huge }

Ordering a Size.Huge here would land on default.