In this lecture, we'll learn how to use a different statement to implement the selection control structure in our code, and that statement is called the switch statement. Let's go take a look at how that works. Our starting point is the code that we wrote for if statement, though I've changed this comment to say switch statements instead. So we're just going to convert our if statement code to use a switch statement instead. All that work will happen down here where we print an appropriate message. So what I'm going to do is I'm just going to comment out our entire if statement. The way we can comment out large blocks of code is with the slash and asterisk and I will take the closing part of that and put it down here instead. So at this point if I run my code, it won't do anything except get the user input. Let's add that switch statement. So we use the keyword "switch." Then between these parentheses, instead of a Boolean expression like we put for our if statements, we put a variable or an expression that we want to switch on, that we want to use for our decision-making. I'm going to say "answer" because that's the variable that is going to determine what message we print. The body of our switch statement also has curly braces. The syntax for this is we list the cases that we want to consider. So here I'll say "case y." The thing I want to do when answer is equal to lowercase y is I want to print out the stuff. So I'm going to steal that and I'll bring it up here. The other thing that I'm going to do is I'm going to put a break, which says after I've done the things inside case y, leave the rest of the switch statement go all the way past the end of the curly brace that shows the end of the body for the switch statement. Now, you will also see some people indenting switch statements differently, where it looks like this instead. That wasn't the default Visual Studio indentation, though that can be changed. I prefer the way this looks, so I'm going to just fix the indentation when I need to, to make it look this way instead. So now if I Control F5, if I enter "yes" like I happen, I get that yes message, and if I enter anything else including "no," I don't get anything. So this is like saying if answer is equal to lowercase y. Let's add our case for n as well. I'll steal this code, I'll change this to n. I'll come down here and I'll steal this code, and I'll put it here instead. So now what's going to happen is it'll check y. If it's y, it will do this. But if it's not, it will check n. If it's n, it will do this, and If it's anything else, it won't do anything. We'll make sure y still works and it does. We'll make sure n now works and it does. Still, that incorrect input doesn't do anything yet with our switch statement. So now we want to add everything else. The problem is that I don't want to say case a, and case b, and case c, and so on. I want something that is similar to an else. Something that says if none of the other things happened, do this. There is such a thing in a switch statement and it's called default. So if I say default, and I say what I want to do, and I include a break here as well. Now, I've covered everything. I've covered yes, I've covered no, and I've covered everything other than yes or no. Let's make sure. Yes works. No works, and key works. This would be a complete translation from the if statement to the switch statement. I do want to show you one more feature of switch statements before we leave. So remember that we decided to use tolower so that we didn't have to have more complicated Boolean expressions, checking both uppercase and lowercase. Let's say I hadn't done that. How can I check here to make sure that I print out this message for an uppercase as well? You might think I'd have to copy the whole case and paste it and change this to an uppercase. But there's actually a better way in switch statement. I can actually include multiple cases. So I can say case capital Y also, and this acts like an or. So stacking up multiple cases basically says if answer is lowercase y or answer is uppercase Y, then print this out. I'll show you that that works by entering an uppercase Y at this point. So the stacking works, and I could do it for N as well, of course. I'll show you that that works. So we can stack our cases if we just want to or them together. If any of them is in fact correct or true, then we do the body of the case. I'm going to comment these out. So you can see them in the final code. But really, converting the answer to lowercase is the better approach for us to take in our coding. Since I've made some changes, I'm now going to check our complete test suite. Lowercase, yes works fine. Uppercase, yes works fine. Lowercase no, uppercase no, and some incorrect input. So that's the translation from an if statement to a switch statement. So you might be wondering at this point, how do you decide between using an if statement and using a switch statement, and largely it's personal preference, at least in a beginning programming course like this. If you're working in a chunk of code that mostly uses if statement, go ahead and use if statements to be consistent with the style in that code. If you're working in a bunch of code that uses switch statements, use switch statements, again, so that the code is consistent. But in general, if you just get to decide, then feel free to decide however you want. To recap, in this lecture, we learned how to implement the selection control structure in our code using the switch statement.