1 00:00:01,260 --> 00:00:07,860 So why do we need Loop's, because there are many cases when you have to do the same things again and 2 00:00:07,860 --> 00:00:14,760 again and again, when I think of Loop's Bart Simpson comes to my mind when he is punished to write 3 00:00:14,760 --> 00:00:22,320 a sentence on the blackboard a hundred times in code, it could be done by copy paste in the same line 4 00:00:22,530 --> 00:00:23,760 a hundred times. 5 00:00:24,120 --> 00:00:28,230 But I think most of us would miscounting after 30. 6 00:00:28,590 --> 00:00:31,230 But it can be done much easier with loops. 7 00:00:31,560 --> 00:00:32,910 What are loops? 8 00:00:33,390 --> 00:00:37,830 Loops are repeating code until a specific condition is met. 9 00:00:38,450 --> 00:00:42,690 In the case of Bart Simpson, the specific condition is reaching a hundred. 10 00:00:43,440 --> 00:00:46,480 Now let's see what we are going to cover in this lecture. 11 00:00:47,250 --> 00:00:49,700 There are a few loop types in Java. 12 00:00:49,740 --> 00:00:56,190 First, we will look into why then do y loop after those for loop. 13 00:00:56,610 --> 00:01:02,680 And finally, I will tell you what and how continue and Brachy words can be used. 14 00:01:02,970 --> 00:01:09,160 The next lecture will be a coding lecture and I will give you examples to all of these. 15 00:01:09,390 --> 00:01:12,310 First, let's look into while loop. 16 00:01:12,330 --> 00:01:22,770 It starts with while then a condition in parentheses and the code that will be executed repeatedly is 17 00:01:22,770 --> 00:01:24,000 in curly braces. 18 00:01:24,390 --> 00:01:29,270 Java will keep continue executing the code until the condition is true. 19 00:01:29,850 --> 00:01:32,010 I think an example make it more clear. 20 00:01:32,460 --> 00:01:32,920 Right. 21 00:01:32,970 --> 00:01:35,580 So we have to print out being right. 22 00:01:35,580 --> 00:01:37,510 Sock's a hundred times. 23 00:01:37,530 --> 00:01:42,800 So first we have to define a variable for the loop strictly outside of it. 24 00:01:43,500 --> 00:01:51,990 We will call it AI and its value will be one then the wire loop with a condition that it should be less 25 00:01:51,990 --> 00:01:54,840 than or equal to a hundred. 26 00:01:54,840 --> 00:01:57,030 And the code in the curly braces. 27 00:01:57,450 --> 00:02:03,120 After we print out the text, we increase the value of AI by one. 28 00:02:03,510 --> 00:02:09,980 After the value of AI is increased, we get back to while and Java checks, whatever. 29 00:02:09,990 --> 00:02:11,730 The condition is still true. 30 00:02:11,970 --> 00:02:17,310 If it's true, it can continue executing the code inside the curly braces. 31 00:02:17,850 --> 00:02:23,100 When the condition is false, it continue executing the code after the loop. 32 00:02:23,610 --> 00:02:29,730 It's really important to keep in mind that the value of I must be modified in the loop. 33 00:02:30,060 --> 00:02:37,170 If you don't modify it, the condition will remain true and the code will never stop and we get into 34 00:02:37,170 --> 00:02:38,310 an infinite loop. 35 00:02:39,120 --> 00:02:44,220 Later, we will use the loops for more complex code than just printing out something. 36 00:02:45,130 --> 00:02:53,560 So now let's move on to do while it's form a bit different from while first do word, then the curly 37 00:02:53,560 --> 00:03:01,160 braces with the code and then the wire keyword with the condition and last a semicolon. 38 00:03:01,720 --> 00:03:06,820 Now, let's see an example we used in case of while transformed into a do. 39 00:03:06,820 --> 00:03:13,810 While the biggest difference between a while and dubal is that while checking the condition before it 40 00:03:13,810 --> 00:03:18,820 starts running it and if it's true, it will start executing the code. 41 00:03:19,300 --> 00:03:26,770 If it's false, then it will skip the code in the curly braces do while runs the code first without 42 00:03:26,770 --> 00:03:31,410 any checks and tests after the first code execution is finished. 43 00:03:31,750 --> 00:03:37,420 So the code will be executed at least once after the conditioned check is finished. 44 00:03:37,420 --> 00:03:41,580 The execution starts again from the beginning of the do while loop. 45 00:03:42,280 --> 00:03:50,370 It's also important to keep an eye on the value of AI and don't let the condition be true all the time. 46 00:03:51,190 --> 00:03:55,990 Moving on to for loop the form of the for loop is the following. 47 00:03:56,470 --> 00:04:02,770 First for key keyword parentheses, then initialization, then semicolon. 48 00:04:02,860 --> 00:04:11,320 Next is a condition, then a semicolon and an updated statement and lost the code in the curly braces. 49 00:04:11,560 --> 00:04:14,380 Let's take our example we used before. 50 00:04:14,830 --> 00:04:25,900 For Int equals one semicolon, I must be lesser or equal to a hundred than semicolon and I plus. 51 00:04:25,900 --> 00:04:27,820 Plus and the code. 52 00:04:29,030 --> 00:04:36,020 The big advantage here is that we don't have to create a variable outside of the loop and we don't need 53 00:04:36,020 --> 00:04:39,440 to increase the value of AI in the loop. 54 00:04:39,470 --> 00:04:46,820 So for Loop is much cleaner because we can do all the administration in one line and it is the best 55 00:04:46,820 --> 00:04:53,060 choice if we know how many iterations we have to do after the code is finished, we get back to the 56 00:04:53,060 --> 00:04:55,400 first line of the for loop. 57 00:04:55,400 --> 00:05:01,100 First, it increases the value of AI and then checks the condition. 58 00:05:01,400 --> 00:05:04,040 If it's true, it keeps running the loop. 59 00:05:04,040 --> 00:05:07,850 If it's false, then it will stop the execution of the loop. 60 00:05:08,300 --> 00:05:14,450 An extra thing here we can put a loop into another loop in case of our example code. 61 00:05:14,660 --> 00:05:18,020 The text will be displayed a thousand times. 62 00:05:18,230 --> 00:05:23,600 First, the inner loop will be executed and the text will be printed out ten times. 63 00:05:24,140 --> 00:05:31,760 The value of I will be increased to and the inner loops code will be executed ten times again. 64 00:05:32,180 --> 00:05:34,760 And it goes on and on and on. 65 00:05:35,270 --> 00:05:38,720 You can also do this with while or do while. 66 00:05:39,020 --> 00:05:41,960 Now the two key words before the coding part. 67 00:05:42,200 --> 00:05:44,830 First one is to continue keyword. 68 00:05:44,990 --> 00:05:50,030 It is actually for skipping steps and loops in our example. 69 00:05:50,030 --> 00:05:56,540 If you use if I know I haven't talked about it, but I think you can imagine what it does. 70 00:05:57,090 --> 00:06:06,290 So the loop goes on and when the value of AI is ten, we skip the tenth print statement, but the loops 71 00:06:06,290 --> 00:06:13,520 execution goes along and at the end ninety nine lines will be printed instead of one hundred because 72 00:06:13,520 --> 00:06:15,350 we skipped at the tenth. 73 00:06:15,590 --> 00:06:17,240 The next one is break. 74 00:06:17,600 --> 00:06:19,610 Break is for different purpose. 75 00:06:19,970 --> 00:06:21,260 It stops the loop. 76 00:06:21,260 --> 00:06:25,630 We use the same code, but I change the continue to break. 77 00:06:26,300 --> 00:06:32,320 So after printing the text nine times the loop will be stopped. 78 00:06:33,500 --> 00:06:40,010 Break and continue are not used very commonly, but there are cases where they are very handy. 79 00:06:40,840 --> 00:06:41,620 That's it. 80 00:06:41,650 --> 00:06:43,420 Let's continue with the coding part.