From the course: Learning C#

Conditionals with "switch" - C# Tutorial

From the course: Learning C#

Conditionals with "switch"

- [Instructor] We've already seen how the if-else statement can be used to make logical decisions and execute different code paths based on the result. The switch statement is another way of making decisions and is usually used when the number of decisions gets to the point where an if-else constructs would be just too cumbersome to write and read. So let's open the code for this example. So we have our same variable, theVal from our if-else example, and we're going to take a look at the switch statement. So the switch statement has a basic form that looks like this. I'm going to write switch, and then some parentheses and then the curly braces. So inside the parentheses is the expression whose value that you want to test. And so I'm going to use this integer variable that we used for the if-else example. So I'm going to switch on theVal. And then to handle each of the decision branches, you define individual case labels inside the switch statement. So for example, I'll have case 50, in which case I would write Console.WriteLine theVal is 50. And then I would have a break statement. And I can have as many of these case labels as I need. So I can add another one. So I'll just copy this. And we need the break statement to finish off each one of these cases. So in this case, I'll change that to 51. So in case the value is 50, then this code will execute. And then the break statement here will cause the entire switch statement to end, so that none of the other cases will execute. I can also group multiple case labels together like this. So if I want it to have the same code handle case 52, whoops, and case 53, and case 54, then I could write Console.WriteLine theVal is between 52 and 54. And of course I have to have a break after that. I can also define a default label, like this. And the default label is sort of like the catch-all else statement. So this indicates what to do when none of the other case labels match the expression. So in this case, I'll just WriteLine theVal is something else. And then once again, I'll have the break. So let's go ahead and try this out. So I'm going to save. And we'll open this up in the terminal. And we'll run this. And you can see that the output says theVal is 50. So that's the case 50 label right here that's getting executed. So now let's change the value to 53. All right and then let's run again. And now you can see that the multiple case label code is being executed. And then finally let's make it 60. All right, and then let's run it one more time. And now you can see that it says that the value is something else. And remember that was the code in our default label here. Now, in this example, I'm using an integer value for the switch expression up here. So this happens to be an integer, but as of C# seven, you can use any non null expression. So this value can be a string. It can be a character, a boolean, an enumeration value. You're not just limited to integers. Let me hide this. So generally speaking, if-else is used for maybe three or four different condition tests, maybe five, just to keep the readability simple. And if you have more than that, then you might want to consider using the switch statement instead which makes code much more readable when you have multiple decision points in one area of the code.

Contents