switch Statement
The switch statement functions similarly to the conditional statements however it is useful in specific situations.
The syntax for the switch statement is the following:
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
case z:
// code block
break;
β¦
default:
// code block
break;
}
The resultant of the expression is compared against the cases x, y, z, and so forth. If it matches a case, it executes the code block of that case. If no case is matched then the default code block is executed if it is provided. The expression is most commonly a variable. We write the break keyword after each case's code block to indicate the end of that case.
Following is an example:
main.cs
1234567891011121314151617181920212223242526272829303132333435using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int score = 5; Console.Write("Grade: "); switch(score) { case 1: Console.WriteLine("Fail"); break; case 2: Console.WriteLine("Pass"); break; case 3: Console.WriteLine("Satisfactory"); break; case 4: Console.WriteLine("Good"); break; case 5: Console.WriteLine("Excellent"); break; default: Console.WriteLine("Invalid"); break; } } } }
You can try changing the value of the variable grade to see the change in the output. The value of grade is compared against each case and the relevant output is displayed.
The same code can be written using if-else statements as well; however in this case the switch statement is more efficient and neater. Whenever we have to compare the result of an expression against many possible values, we use the switch statement.
1. What is the primary purpose of using a switch statement in programming?
2. In the provided C# example, what will be the output if the variable score is set to 3?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you show me an example using the switch statement?
What happens if I forget to use the break statement in a case?
When should I use a switch statement instead of if-else?
Awesome!
Completion rate improved to 1.59
switch Statement
Swipe to show menu
The switch statement functions similarly to the conditional statements however it is useful in specific situations.
The syntax for the switch statement is the following:
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
case z:
// code block
break;
β¦
default:
// code block
break;
}
The resultant of the expression is compared against the cases x, y, z, and so forth. If it matches a case, it executes the code block of that case. If no case is matched then the default code block is executed if it is provided. The expression is most commonly a variable. We write the break keyword after each case's code block to indicate the end of that case.
Following is an example:
main.cs
1234567891011121314151617181920212223242526272829303132333435using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int score = 5; Console.Write("Grade: "); switch(score) { case 1: Console.WriteLine("Fail"); break; case 2: Console.WriteLine("Pass"); break; case 3: Console.WriteLine("Satisfactory"); break; case 4: Console.WriteLine("Good"); break; case 5: Console.WriteLine("Excellent"); break; default: Console.WriteLine("Invalid"); break; } } } }
You can try changing the value of the variable grade to see the change in the output. The value of grade is compared against each case and the relevant output is displayed.
The same code can be written using if-else statements as well; however in this case the switch statement is more efficient and neater. Whenever we have to compare the result of an expression against many possible values, we use the switch statement.
1. What is the primary purpose of using a switch statement in programming?
2. In the provided C# example, what will be the output if the variable score is set to 3?
Thanks for your feedback!