1 00:00:00,450 --> 00:00:05,460 Which demons can also give you control over your code, you learn to control which parts of your code 2 00:00:05,460 --> 00:00:12,300 run using, if elusive, and now statements, each statement has a condition and Java looks at the first 3 00:00:12,300 --> 00:00:15,320 statement with a true condition and runs the code inside. 4 00:00:16,230 --> 00:00:19,680 You can also use switch statements to control how your code runs. 5 00:00:20,310 --> 00:00:25,110 So in this lesson, you're going to learn how do you switch as well as when do you switch versus if 6 00:00:25,110 --> 00:00:25,680 else? 7 00:00:27,890 --> 00:00:32,390 First thing I'll need you to do is create a new class by yourself inside the Section three project, 8 00:00:32,390 --> 00:00:37,580 create a new file name, switch statements Java, and make sure the class has a main method. 9 00:00:43,010 --> 00:00:48,740 So how do you switch a switch statement compares one value against a list of values. 10 00:00:49,370 --> 00:00:53,510 In other words, it compares an argument against a list of cases. 11 00:00:54,530 --> 00:01:00,420 The argument is the value that you're comparing and if the argument matches value one, the first case 12 00:01:00,460 --> 00:01:06,800 runs, if the argument matches value to the second case runs, if the argument matches value three, 13 00:01:06,800 --> 00:01:12,770 the third case runs and if the argument matches none of the values the default case runs. 14 00:01:14,740 --> 00:01:18,370 And so back in our code, we're going to see this in action, we're going to make it into variable named 15 00:01:18,370 --> 00:01:24,370 and month and it's going to story value that represents a month five, which corresponds to the month 16 00:01:24,370 --> 00:01:24,850 of May. 17 00:01:25,720 --> 00:01:30,880 And so now we're going to make a switch statement and it's going to compare your month value against 18 00:01:30,880 --> 00:01:33,240 the list of month values in the form of cases. 19 00:01:33,730 --> 00:01:35,230 So we're going to rate case one. 20 00:01:39,600 --> 00:01:44,820 And there are 12 months, so we'll need 12 cases, so I'm going to copy this 11 more times. 21 00:01:49,600 --> 00:01:52,570 And I'm going to number each case in ascending order. 22 00:02:06,040 --> 00:02:12,970 Followed by default, now, if the argument month matches one of the cases, that case is going to run 23 00:02:12,970 --> 00:02:15,370 and we're going to need to print that month in text. 24 00:02:16,030 --> 00:02:20,770 So if the month ends up being equal to one, we're going to print January. 25 00:02:21,760 --> 00:02:24,550 If it matches, case two will print. 26 00:02:29,480 --> 00:02:32,750 And you know what, I'm just going to copy this Prince theme throughout. 27 00:02:48,980 --> 00:02:52,100 And now I'm going to fill in the appropriate month values. 28 00:03:19,550 --> 00:03:24,540 And if I were a month value doesn't match any of our cases, a default case should execute. 29 00:03:25,130 --> 00:03:30,660 And if it does, we're going to print please enter a valid month. 30 00:03:32,780 --> 00:03:34,490 They think we're ready to run our code. 31 00:03:46,440 --> 00:03:47,970 Whoa, that's weird. 32 00:03:48,750 --> 00:03:55,710 The month argument matches Case five, Jalaa clearly ran Case five, but it also runs every case that 33 00:03:55,710 --> 00:03:56,350 follows. 34 00:03:57,150 --> 00:04:02,820 Well, that's because by nature, which runs every case that follows the case much, it's like the switch 35 00:04:02,820 --> 00:04:04,820 turns on when there's a match. 36 00:04:05,610 --> 00:04:06,570 Let's have a look. 37 00:04:06,600 --> 00:04:08,400 You saw the value five in the month. 38 00:04:08,400 --> 00:04:14,400 Variable switch accepts your month value as an argument, and it compares it to a list of month values. 39 00:04:14,820 --> 00:04:17,589 It starts comparing your value against each case. 40 00:04:17,940 --> 00:04:22,200 Case one, two, three, four, five. 41 00:04:22,200 --> 00:04:22,580 Aha. 42 00:04:22,590 --> 00:04:23,670 We have a match. 43 00:04:24,740 --> 00:04:31,550 So the switch turns on and Javor runs case five, but it also runs every case that follows the case 44 00:04:31,550 --> 00:04:32,300 match. 45 00:04:37,180 --> 00:04:38,540 This is pretty frustrating. 46 00:04:38,560 --> 00:04:39,720 How do we fix it? 47 00:04:42,890 --> 00:04:48,320 That is exactly why we have the brake keyboard, the brake keyboard can break the switch statements. 48 00:04:50,380 --> 00:04:56,170 And so every case inside your switch them it needs to end with the keyword that way after your case 49 00:04:56,170 --> 00:04:59,110 runs, Java knows to break out of the switch statement. 50 00:05:00,160 --> 00:05:02,680 So back in our code, we're going to fix the switch statements. 51 00:05:02,680 --> 00:05:05,560 We're going to add the break keyword at the end of each case. 52 00:05:20,990 --> 00:05:22,520 And now we can rerun our code. 53 00:05:31,190 --> 00:05:32,140 And that's much better. 54 00:05:35,450 --> 00:05:37,930 So which compares your value against each case? 55 00:05:38,120 --> 00:05:38,520 Uh huh. 56 00:05:38,560 --> 00:05:45,670 We found a match after Jova runs the code inside case five, it then sees the break keyword and that 57 00:05:45,670 --> 00:05:47,410 breaks out of the switch statements. 58 00:05:51,660 --> 00:05:54,040 So why didn't we at break next to default? 59 00:05:54,870 --> 00:05:57,230 Well, default is the last thing that can run. 60 00:05:57,280 --> 00:06:01,590 Switch already breaks after default, but for the sake of example. 61 00:06:02,990 --> 00:06:06,470 We'll set month equal to some absurd number like 30 to. 62 00:06:10,900 --> 00:06:12,160 We'll rerun our code. 63 00:06:13,940 --> 00:06:18,920 And the month value doesn't match any of the cases, so the default case Rut's. 64 00:06:22,300 --> 00:06:24,570 All right, so it's time for the million dollar question. 65 00:06:24,610 --> 00:06:25,960 When do we use a Felsen? 66 00:06:25,960 --> 00:06:27,180 When do we use switch? 67 00:06:27,840 --> 00:06:31,450 I want to preface this by saying I use a false 95 percent of the time. 68 00:06:32,020 --> 00:06:37,480 And if statement can test any condition, a switch statement can only compare a value against the list 69 00:06:37,480 --> 00:06:38,230 of others. 70 00:06:41,940 --> 00:06:47,610 So we'll think back to the OP we just built, the one that grants diploma if a student has over 40 credits 71 00:06:47,850 --> 00:06:50,490 and a minimum GPA of 2.0. 72 00:06:51,330 --> 00:06:58,140 Can you think of a way to do this using such absolutely impossible GPA and credits can be any number 73 00:06:58,140 --> 00:07:00,170 from an infinite range of values. 74 00:07:00,660 --> 00:07:02,640 Are you going to make infinite cases? 75 00:07:03,030 --> 00:07:03,870 I don't think so. 76 00:07:04,650 --> 00:07:07,860 The way it fails is designed around conditions and ranges. 77 00:07:07,870 --> 00:07:12,480 It's way more flexible than comparing a value against the list of cases. 78 00:07:13,320 --> 00:07:18,760 So if you fast forward to the AP book just now, you can easily use it false. 79 00:07:19,470 --> 00:07:20,730 So why didn't we use them? 80 00:07:21,960 --> 00:07:25,700 We'll have a look at this code looks pretty ugly, doesn't it? 81 00:07:26,680 --> 00:07:33,230 Once again, the screaming cat is there to tell us what not to do if you're comparing one value against 82 00:07:33,230 --> 00:07:39,460 the list of other values, don't use a false uSwitch because it's honestly prettier. 83 00:07:39,490 --> 00:07:40,600 It's easier to read. 84 00:07:40,840 --> 00:07:44,400 And they also run a tiny bit faster than anything else in general. 85 00:07:45,370 --> 00:07:49,140 So if you don't want to get lost, just follow this rule of thumb and you should be golden. 86 00:07:49,660 --> 00:07:55,740 You can only use switch to compare one value against the list of others in any other scenario. 87 00:07:55,740 --> 00:08:00,760 And 95 percent of the time you're going to need efforts to control how your code runs. 88 00:08:02,830 --> 00:08:08,210 In this section, you learn to fully control how your code runs using NFLs and switch statements, if 89 00:08:08,220 --> 00:08:09,340 false runs a piece of code. 90 00:08:09,340 --> 00:08:14,710 If the condition is true, conditional statements, officials are way more flexible than switch statements. 91 00:08:15,130 --> 00:08:19,750 Ninety five percent of the time you're going to use efforts to control how your code runs. 92 00:08:20,460 --> 00:08:21,700 Switch runs a piece of code. 93 00:08:21,700 --> 00:08:27,550 If the argument matches a case on the off chance that you need to compare a value against a list of 94 00:08:27,550 --> 00:08:30,160 other values, then you can use switch.