1 00:00:00,900 --> 00:00:07,740 This lecture is about control flow, so when and why we need control flow, there are many cases when 2 00:00:07,740 --> 00:00:10,620 you have to act differently based on the user input. 3 00:00:10,830 --> 00:00:14,230 Let's take Who Wants to Be a Millionaire as an example. 4 00:00:14,790 --> 00:00:23,010 There is a question on every level with four options and only one right answer is that if the player 5 00:00:23,010 --> 00:00:29,050 chooses the right one, the bottom will start glowing green and can go to the next level. 6 00:00:29,700 --> 00:00:34,420 Otherwise, the bottom will glow orange or red and the player will lose the game. 7 00:00:34,800 --> 00:00:38,280 So based on the decision, different code will be run. 8 00:00:39,120 --> 00:00:47,490 So what contraflow as it is used for decision making and it enables your program to conditionally execute 9 00:00:47,490 --> 00:00:48,270 code blocks. 10 00:00:49,690 --> 00:00:57,310 OK, what we are going to look into in this lecture first, the if then the if our statements with some 11 00:00:57,310 --> 00:01:04,630 examples and lost the switch statement after this lecture, there is going to be according to. 12 00:01:05,970 --> 00:01:14,790 So how and if statement looks like first the key word, then a condition in parentheses, then the curly 13 00:01:14,790 --> 00:01:22,930 braces with the code, the condition can be a boolean variable, a comparison or a complex condition. 14 00:01:23,280 --> 00:01:27,810 Let's see some examples in case of the damaged car, it would look like this. 15 00:01:29,030 --> 00:01:35,960 If the car is damaged, we print out damaged car after car is not damaged, then nothing is printed 16 00:01:35,960 --> 00:01:36,290 out. 17 00:01:37,250 --> 00:01:45,100 Another one, if the car's horsepower is greater than 500, the reaction would be then this car is fast 18 00:01:45,740 --> 00:01:47,780 and let's see a bit more complex. 19 00:01:48,380 --> 00:01:57,710 If the move is rating is greater than six point eight and the movie category is action or comedy, I 20 00:01:57,710 --> 00:01:59,510 say I would watch it tonight. 21 00:02:00,630 --> 00:02:08,220 The movies category is Prentice's because an operator has higher precedence than or operator like in 22 00:02:08,220 --> 00:02:13,650 math, multiplication has higher precedence than addition or subtraction. 23 00:02:13,980 --> 00:02:19,770 Now, let's move on to if else, if else is an extension of if statement. 24 00:02:20,460 --> 00:02:22,950 It gives us a default option. 25 00:02:22,950 --> 00:02:29,370 If the condition is false, if the condition is true, the code belongs to F. will be executed. 26 00:02:29,670 --> 00:02:33,390 And after that the execution continues after the whole. 27 00:02:33,390 --> 00:02:42,720 If our statement if the condition is false, then Java executes the code which belongs to us and continue 28 00:02:42,720 --> 00:02:45,330 executing the code after that. 29 00:02:46,080 --> 00:02:52,440 In the case of is damaged, if the car is damaged, damaged car is printed out, otherwise accident 30 00:02:52,440 --> 00:02:53,940 free text will appear. 31 00:02:54,950 --> 00:02:59,400 In the case of horsepower, one, the cost horsepower is less than 500. 32 00:02:59,810 --> 00:03:01,960 We would get a reaction, something like this. 33 00:03:02,000 --> 00:03:03,170 Well, not that bad. 34 00:03:03,690 --> 00:03:11,420 And if the movie's rating is less than six point eight or it's not an action or comedy, I would say 35 00:03:11,690 --> 00:03:12,830 no way to watch it. 36 00:03:13,790 --> 00:03:20,690 We also have another form of if us in this case, after the first conditional check, we have another 37 00:03:20,690 --> 00:03:21,050 check. 38 00:03:21,170 --> 00:03:24,620 If the first one is false, Thanjavur checks the second one. 39 00:03:24,830 --> 00:03:30,590 If that's false, too, then the execution continues after the if as block. 40 00:03:30,900 --> 00:03:37,670 Let's make it clear within an example in this case, which are the color of the car, and we print out 41 00:03:37,670 --> 00:03:44,620 the text based on that, if the car's color equals to red, we print out it is a red car. 42 00:03:45,080 --> 00:03:50,720 If that's not a red car, which checks, whether it's a green car, if it's true, it is. 43 00:03:50,720 --> 00:03:55,040 A green car is printed out, otherwise nothing is printed out. 44 00:03:55,730 --> 00:03:58,850 We can also add an assumption to this. 45 00:03:59,330 --> 00:04:05,220 We can have a multiple as ifs here and at the end we have analysis. 46 00:04:05,960 --> 00:04:13,880 So in the case of the car color, we can check whether the car is red or green or orange or black. 47 00:04:13,880 --> 00:04:21,440 We print out something that shows what the color of the car and have else for the case. 48 00:04:21,440 --> 00:04:23,360 We cannot identify the color. 49 00:04:24,510 --> 00:04:29,190 If it's not clear, don't worry, we will also cover these in the cording part. 50 00:04:29,880 --> 00:04:32,250 Now let's move on to switch statement. 51 00:04:33,470 --> 00:04:41,330 The switch is to select one of many code blocks to be executed, its form is switch keyword a value 52 00:04:41,330 --> 00:04:42,490 in parentheses. 53 00:04:42,800 --> 00:04:48,890 We have cases with values that can be compared to the first value. 54 00:04:50,000 --> 00:04:53,450 We also have break and the fourth keywords here. 55 00:04:53,750 --> 00:05:02,710 Now let's go into details so the value is compared with the values of each case like case one case to 56 00:05:02,710 --> 00:05:05,050 see if there is a match. 57 00:05:05,270 --> 00:05:08,810 The associated block of code is executed. 58 00:05:09,110 --> 00:05:14,480 When Java reaches a breaking word, it breaks out of the switch block. 59 00:05:14,810 --> 00:05:23,020 This will stop the execution of more code and case testing inside the block before part is executed 60 00:05:23,030 --> 00:05:25,250 when no case match is found. 61 00:05:25,620 --> 00:05:27,620 Let's take the car again. 62 00:05:27,620 --> 00:05:32,060 As an example, we give the car color to the switch statement. 63 00:05:32,390 --> 00:05:35,000 Java compares it to the cases. 64 00:05:35,180 --> 00:05:39,170 If there is a match, the right text is printed out. 65 00:05:39,470 --> 00:05:46,490 If there is no match, then I have no idea what the color of this car is printed out, which is better 66 00:05:46,490 --> 00:05:53,390 in cases when we have a lot of options and a code that belongs to that is quite short. 67 00:05:53,660 --> 00:05:55,220 One line or two lines. 68 00:05:55,970 --> 00:05:59,480 In case of more lines, it would make the code less readable. 69 00:06:00,740 --> 00:06:01,400 That's it. 70 00:06:01,670 --> 00:06:03,460 Let's continue with the coding part.