 Hey everybody, it's Brian. Today we're going to be examining the switch statement. Let's dive right in, make int x equals a, let's just pick a number 5. Now what is a switch statement? Help if I could spell it. Switch statement allows you to examine a variable and then perform actions based on the number. So we're going to say break. Now what is this? This is some weird syntax we have not seen before here. Switch and then in parentheses our variable and then our code block and inside our code block we have a case with a number, a colon and then the break statement. What's the break statement do? The break statement simply breaks out of this code block. It's like a prisoner in jail and you're trying to break out of jail. So it'll take the context and break out of this code block right here. Now case zero, what does that mean? Case zero is we're saying if x is equal to zero. So you could quite literally say if x equals zero then do whatever is between case and break. So just say system. I really need to buy a new keyboard here. And we're just going to say zero. But you can see x is five. So let's actually take this copy paste and we're going to just take a couple of these and we'll say zero, five and nine. File, I was thinking of a different tutorial bear with me here. And nine. So int x equals five, switch x. So we're going to say switch. Think of these as light switches. You're going to look at the value of x and you're going to say is it zero? No, is it five? Yes. Then flip this switch on which is this code block. Now the break statement keeps you from doing what's called follow through. A follow through is if you got rid of that break statement it would say is x zero? No, is it five? Yes, execute this. But then it would jump right back down into here. We don't want that. So we just want to put the break statement in there. Let's run this, see what happens. And you see it spits out five. Yes, you might have guessed this is just a fancy way of doing an F statement. The switch command is very simple, very quick, easy to use. It's very common in code so I suggest you get familiar with it. Typically they're used with enumerators, which we haven't covered yet but we will. You cannot use a switch statement with a string. So to test that let's go string. Oops, m equals hello. You cannot do switch m. See, it already starts complaining. Cannot switch in value type of string. Only convertible int values or num constants are permitted. Some other languages allow you to actually use the switch command on a string and it comes in pretty handy but Java does not. One other thing we're going to cover is the default. Default is similar to a case except for it. It is triggered when none of the other cases match. So let's just say x is equal to 200. Let's get rid of that string. And we're going to go in here and it's going to say switch x. Is it 0, no, 5, no, 9, no. Then it's going to call default. And it's going to say what are you talking about here? See, layer prints. So that is the switch statement in a nutshell. Not a very complex statement. You might be wondering why you need the switch statement. Well it makes things a little simpler. I mean otherwise you'd be writing a lot of if then statements over and over again. This is Brian. I thank you for watching. I hope you found this video educational and entertaining.