1 00:00:00,760 --> 00:00:07,300 In the scolding lecture, we are going to use all the loops we went through in the previous video first 2 00:00:07,300 --> 00:00:11,910 while then do while for loop, then continue and break. 3 00:00:11,920 --> 00:00:17,020 And finally, we take an example for a for loop in a full loop. 4 00:00:17,740 --> 00:00:21,150 We're going to use Bart Simpson's punishment as an example. 5 00:00:21,670 --> 00:00:23,350 So let's do it first. 6 00:00:23,350 --> 00:00:25,720 Let's create a new class for this. 7 00:00:25,870 --> 00:00:26,260 Right. 8 00:00:26,260 --> 00:00:27,940 Click on one of the files here. 9 00:00:29,170 --> 00:00:39,730 You Java class loops up, copied the main method from one of the files you have been created. 10 00:00:44,780 --> 00:00:50,960 Copy and paste it here, it entered the other curly brace. 11 00:00:50,990 --> 00:00:52,860 OK, let's start with wild. 12 00:00:52,880 --> 00:00:57,810 So as I said, we are going to need a variable outside of the loop. 13 00:00:58,580 --> 00:01:06,060 Call it I and I equals one and a semicolon. 14 00:01:06,200 --> 00:01:12,890 By the way, we use letters for such variables because they don't have any exact meaning. 15 00:01:13,160 --> 00:01:21,300 They just helping to run our code five or fifty or a hundred times next to the wire. 16 00:01:22,430 --> 00:01:31,790 And in the parentheses, I should be less than or equal to let's use five here instead of a hundred, 17 00:01:31,940 --> 00:01:41,420 then curly braces enter system that order print double quotes. 18 00:01:43,540 --> 00:01:56,270 Being right sucks, one very important thing here is to increase the value of I won, so I lost plus 19 00:01:56,290 --> 00:02:03,970 and a semicolon, I think it would be useful to use I and added to the text just to see how many lines 20 00:02:03,970 --> 00:02:04,960 we printed out. 21 00:02:05,260 --> 00:02:05,890 So 22 00:02:08,620 --> 00:02:10,690 I plus. 23 00:02:12,090 --> 00:02:18,240 A dot and the space here also print out the value of eye after the loop. 24 00:02:18,480 --> 00:02:19,170 So. 25 00:02:21,840 --> 00:02:33,450 Double quotes, well, you have a colon plus I let's add a blank line to. 26 00:02:36,740 --> 00:02:39,590 The latest now round the code. 27 00:02:41,840 --> 00:02:49,700 All right, being socks printed out five times and the value of I is six, because that's one, Java 28 00:02:49,700 --> 00:02:51,500 stopped executing the loop. 29 00:02:51,890 --> 00:02:53,610 Great, it works as it should. 30 00:02:53,630 --> 00:02:56,120 Now, let's do it with the wire loop. 31 00:02:56,490 --> 00:02:59,630 Now we have to create a new variable for this. 32 00:02:59,630 --> 00:03:13,100 Let's call it J and J equals one and a semicolon, starting with the curly braces computer code from 33 00:03:13,100 --> 00:03:13,580 about. 34 00:03:15,040 --> 00:03:16,140 Paste it here. 35 00:03:17,090 --> 00:03:30,590 Change the variable name to Jay and here to now wild parentheses, Jay, less than or equal to five 36 00:03:30,890 --> 00:03:36,410 and a semicolon here copied these lines to pasted it here. 37 00:03:37,860 --> 00:03:43,770 Change the text and the variable name to run the code, brilliant. 38 00:03:43,860 --> 00:03:49,620 Same result now change the five to zero and both loops. 39 00:03:50,160 --> 00:03:51,960 So here. 40 00:03:54,010 --> 00:03:57,610 And here now, we're on the road again. 41 00:04:00,320 --> 00:04:00,990 Nice. 42 00:04:01,310 --> 00:04:10,100 So as I said, while check the condition first, and if it's false, Java doesn't execute the loop in 43 00:04:10,100 --> 00:04:16,010 the case of do while it runs the code first, then checks the condition. 44 00:04:16,400 --> 00:04:19,370 You can leave it as it is or you can change it back. 45 00:04:19,520 --> 00:04:20,750 It's totally up to you. 46 00:04:22,190 --> 00:04:33,860 Now, let's move on to for loop, we don't need an external variable here, so for parentheses and I 47 00:04:34,580 --> 00:04:39,830 equals one and a semicolon and we get an error message here. 48 00:04:39,830 --> 00:04:47,480 Variable I is already defined in the scope, so I has already been defined outside of this. 49 00:04:47,780 --> 00:04:50,000 So let's use another variable name. 50 00:04:51,120 --> 00:05:06,180 OK, now, OK, should be less than or equal to five, then a semicolon and Kate plus plus then curly 51 00:05:06,180 --> 00:05:10,440 braces hit, enter and copy. 52 00:05:12,350 --> 00:05:13,280 This line. 53 00:05:14,890 --> 00:05:25,210 To hear change the variable name Touquet, and that's it, now compare these lines to copy and paste 54 00:05:25,210 --> 00:05:30,450 it here, change the text Touquet and variable name as well. 55 00:05:32,480 --> 00:05:40,340 OK, so what's the problem here, it says cannot resolve symbolic, it's right because K is only accessible 56 00:05:40,340 --> 00:05:41,480 inside the loop. 57 00:05:41,780 --> 00:05:44,960 So delete that line and run the code. 58 00:05:52,330 --> 00:06:00,010 Everything is fine, the Texas printed out five times, move on, continue and break and use our fresh 59 00:06:00,010 --> 00:06:04,060 and crisp for loop at and if. 60 00:06:05,790 --> 00:06:21,540 Here, parentheses and K equals free curly braces and continue keyword now run the code again. 61 00:06:23,170 --> 00:06:27,100 So as you can see, the third step is missing Snice. 62 00:06:28,200 --> 00:06:30,780 Now replace the continue with break. 63 00:06:33,470 --> 00:06:34,670 And run the code again. 64 00:06:35,890 --> 00:06:41,530 Now, as you can see, the execution stopped at the third step and two lines are printed out. 65 00:06:43,010 --> 00:06:49,220 Last, let's do a bit weird thing, put a loop into another loop, copied a for loop. 66 00:06:52,630 --> 00:06:55,120 And remove the F from it. 67 00:06:57,320 --> 00:06:58,690 Now, copy it again. 68 00:07:01,860 --> 00:07:05,820 And posted here instead of the system that our DOT printed on. 69 00:07:06,790 --> 00:07:09,850 Now, as you can see, we should change the key here. 70 00:07:11,180 --> 00:07:18,950 Because it's already used, so let's rename it to out here to. 71 00:07:20,870 --> 00:07:30,800 And here change the printed out text to so double quotes, OK, because. 72 00:07:31,960 --> 00:07:34,660 Then a plus an. 73 00:07:36,540 --> 00:07:43,770 Space of equals plus L and the plus sign again. 74 00:07:45,480 --> 00:07:51,330 And later, that changed the condition of Al from five to two. 75 00:07:51,840 --> 00:07:53,130 Now let's run our code. 76 00:07:56,380 --> 00:08:05,650 So the value of K and L is one than the inner loop is executed again and the value of L is increased 77 00:08:05,650 --> 00:08:10,450 to two, then the value of K is increased by one. 78 00:08:11,020 --> 00:08:15,700 And then the inner loop starts from the beginning. 79 00:08:15,850 --> 00:08:18,020 And this goes on and on and on. 80 00:08:18,880 --> 00:08:21,820 Finally, the text is printed out ten times. 81 00:08:23,210 --> 00:08:30,260 We will definitely use loops later for more complex tasks, if you have any questions, feel free to 82 00:08:30,260 --> 00:08:30,870 ask me. 83 00:08:31,360 --> 00:08:32,690 Soon the next lecture.