Switch Statement
Part of the Fundamentals section of Coddy's C# journey — lesson 27 of 69.
The switch statement is like a multi-way if statement. Instead of evaluating a single condition, it checks the value of a variable against multiple cases and executes the code associated with the matching case.
Here's the basic structure of a switch statement:
switch (variable) {
case value1:
// Code to execute if variable equals value1
break;
case value2:
// Code to execute if variable equals value2
break;
// ... more cases
default:
// Code to execute if no case matches
break;
}- The
switchkeyword is followed by the variable you want to test in parentheses. - Each
caserepresents a possible value of the variable. - The code inside each
caseis executed if the variable matches that case's value.
- The
breakstatement is crucial; it exits theswitchafter a case is executed. Without it, execution would "fall through" to the next case.
- The
defaultcase is optional and is executed if no other case matches.
Here's an example:
int day = 3;
string dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
// ... cases for other days
default:
dayName = "Invalid day";
break;
}You can also combine multiple cases into one:
int day = 3;
string dayName;
switch (day) {
case 1:
case 2:
case 3:
dayName = "Start of week";
break;
// ... cases for other days
default:
dayName = "Invalid day";
break;
}Challenge
BeginnerCreate a program that takes a month number (1 for January, 2 for February, etc.) and prints the season it belongs to. Use a switch statement for the logic.
The seasons and their corresponding months are:
- Winter: December (12), January (1), February (2)
- Spring: March (3), April (4), May (5)
- Summer: June (6), July (7), August (8)
- Autumn: September (9), October (10), November (11)
Cheat sheet
The switch statement is a multi-way conditional that checks a variable against multiple cases:
switch (variable) {
case value1:
// Code to execute if variable equals value1
break;
case value2:
// Code to execute if variable equals value2
break;
default:
// Code to execute if no case matches
break;
}Key components:
case: represents a possible value to matchbreak: exits the switch after a case executes (prevents fall-through)default: optional case executed when no other case matches
You can combine multiple cases:
switch (day) {
case 1:
case 2:
case 3:
dayName = "Start of week";
break;
default:
dayName = "Invalid day";
break;
}Try it yourself
using System;
public class Program {
public static void Main(string[] args) {
int month = int.Parse(Console.ReadLine());
string season = "";
// Write your code below
Console.WriteLine(season);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic Shortcuts5Operators Part 2
Comparison OperatorsLogical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 36Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else