From the course: Learning C#

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Conditionals with "switch"

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…

Contents