C# switch
int choice = 2; stores the whole number 2. Then switch (choice) compares that number to fixed labels. When choice is 2, case 2 runs and Console.WriteLine("mid") writes mid. The screen is one line. Keep the public class named Pine.
One value, several fixed labels
Open the Pine file. The class name stays Pine. This page does not rename the type. Delete bool ready if that line is still there, including if (ready), pass, wait, and every else block from the previous page. Delete bool both if that line is still there. Delete any ReadLine line. The file you keep has no wait for typing, no ready, and no if / else fork. The screen is written only by a writing line under the matched case or under default.
Inside Main, store a whole number before the switch: int choice = 2;. int is the kind word. choice is the name, and it starts with a small c. There are no quotes around 2. The semicolon ends the storing line. That line does not write the screen. After that comes the switch itself:
switch (choice) {
case 1:
Console.WriteLine("low");
break;
case 2:
Console.WriteLine("mid");
break;
case 3:
Console.WriteLine("high");
break;
default:
Console.WriteLine("none");
break;
}
The word switch is lowercase. Inside the parentheses sits the name choice — the value C# will compare to each case label. Each case line ends with a colon, not a semicolon. Under case 2 sits Console.WriteLine("mid"); then the word break and a semicolon. When choice holds 2, C# enters case 2, writes mid, hits break, and leaves the whole switch. case 3 and default do not run on this turn. Save the file.
The file you keep
Keep this teaching shape. One kind word, int. One name, choice, stored as 2. One switch with parentheses around choice. Cases 1, 2, and 3, each with a writing line and a break. One default that writes none. There is no ready line, no if, and no bare writing of the digit 2 outside the cases.
using System;
public class Pine
{
public static void Main()
{
int choice = 2;
switch (choice)
{
case 1:
Console.WriteLine("low");
break;
case 2:
Console.WriteLine("mid");
break;
case 3:
Console.WriteLine("high");
break;
default:
Console.WriteLine("none");
break;
}
}
}
Read it from the top. using System; prepares short names. public class Pine names the type. public static void Main() is where the runtime starts. int choice = 2; stores 2. It writes nothing. switch (choice) begins the compare. case 2 matches, so Console.WriteLine("mid"); runs, then break; leaves the switch. One line on the screen: mid. No quotes on the screen. No second line. There is no bool ready line in the file you keep.
Run it with dotnet run. The program writes mid. It does not wait for you to type. The stored number and the switch were already in the file. If dotnet run waits, ReadLine is still in the file. Delete that line and keep int choice = 2; and the switch above. The program finishes after the chosen writing line. The screen shows mid. One line. If the screen shows 2, you are still writing the number itself. If the screen shows pass or wait, the if / else file is still in place. Delete the ready fork. If the screen shows none, choice is a value that matches no case — that is the second file on this page, not the file you keep. If the screen shows switch (choice) as letters, a writing line has quotes around those characters. You do not reinstall the .NET SDK for a screen that shows the wrong line.
Why mid and not low or high? Because the value in parentheses was 2, and case 2 is the label that matches. C# does not write the digit 2 on this run unless you write a writing line for that digit. It writes whatever writing line runs under the matched case. Parentheses hold the value being compared. Each case label is a fixed whole number here. break ends the switch so later cases stay quiet. default is waiting for values that match none of the cases — it stays unused while choice is 2.
When no case matches
Change only the stored value: int choice = 9;. Leave the switch structure as it is. Now none of case 1, case 2, or case 3 matches. C# moves to default and runs Console.WriteLine("none");. The screen should show none, one line.
using System;
public class Pine
{
public static void Main()
{
int choice = 9;
switch (choice)
{
case 1:
Console.WriteLine("low");
break;
case 2:
Console.WriteLine("mid");
break;
case 3:
Console.WriteLine("high");
break;
default:
Console.WriteLine("none");
break;
}
}
}
This is the second file. It is not the file you keep. Run dotnet run. The screen shows none. One line. It does not show mid. It does not show 9 unless you write that digit on purpose. If the screen shows mid, you are still on the file you keep, where choice is 2. After you have seen none, put the kept file back: int choice = 2; and the same switch that writes mid on case 2. Both versions are valid C#. They teach the same switch shape with a matching case and with default.
Read the default run once more in order. choice holds 9. switch (choice) begins. case 1 does not match. case 2 does not match. case 3 does not match. Control continues at default. Console.WriteLine("none"); executes. One line on the screen. The digit 9 itself never appears unless you write it on purpose.
break stops later cases
break is not decoration. Without it, C# can keep running the next case’s lines after a match. That fall-through surprises beginners. Here is a short demo that forgets break on purpose after case 2 — it is not the file you keep:
using System;
public class Pine
{
public static void Main()
{
int choice = 2;
switch (choice)
{
case 1:
Console.WriteLine("low");
break;
case 2:
Console.WriteLine("mid");
case 3:
Console.WriteLine("high");
break;
default:
Console.WriteLine("none");
break;
}
}
}
With choice equal to 2 and no break under case 2, the screen can show mid on one line and high on the next. case 2 matched, wrote mid, then fell into case 3 and wrote high. The file you keep puts break under every case so each path writes exactly one line. Put break back before you treat the Pine file as the kept lesson file again.
Rules that keep the labels clear
Parentheses around the switched value are required. Writing switch choice { … } without parentheses makes the build write an error. Each case label on this page is a whole-number constant: case 1:, case 2:, case 3:. A colon follows the label. Writing case 2; with a semicolon instead of a colon is not the shape this lesson builds. default has no value after it — just default: then the lines for “nothing matched.”
switch is not the same tool as if. if (ready) asks a true-or-false question. switch (choice) matches one value against a list of fixed labels. Do not write switch (ready) with a bool and expect the same lesson shape as the previous page. Keep this page’s demos on an int named choice and whole-number case labels. Comparisons such as if (score > 10) still belong to if when you need a range; switch shines when the values are discrete and known in advance.
A common mix-up is writing the syntax itself: Console.WriteLine("switch (choice)");. That writes the characters switch (choice). It does not pick a case. Another mix-up is leaving an if / else demo in the same Main and wondering why the screen shows pass instead of mid. This session’s Main should hold the int and the switch only.
📊 choice and the screen
| choice | Screen |
|---|---|
| 1 | low |
| 2 | mid |
| 3 | high |
| 9 | none |
Use the table as a check after a run, not instead of one. choice 2 with the file you keep writes mid. choice 9 with the second file writes none. choice 1 writes low. choice 3 writes high. If mid and high both appear for choice 2, a break is missing under case 2 — fix the breaks rather than reinstalling the .NET SDK.
You can now store a whole number, put it in switch (…), and give each fixed value its own case with a writing line and a break. When a case matches, that line runs; when nothing matches, default runs. The next page is for, where the same block of lines can run again and again while a counter changes.
The screen is the matched case
The file you keep writes mid. The name choice is in the parentheses, and choice is not on the screen. The digit 2 is in the storing line, and 2 is not on the screen. The other cases are in the file, and they do not run on the kept run. Quotes around switch (choice) write letters, not a matched case.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top