Switch-sætning
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:
main.cs
12345678910111213141516switch(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?
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you show me the syntax for the switch statement?
Can you provide an example of a switch statement in code?
What are some common use cases for using a switch statement?
Awesome!
Completion rate improved to 1.59
Switch-sætning
Stryg for at vise menuen
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:
main.cs
12345678910111213141516switch(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?
Tak for dine kommentarer!