1 00:00:00,009 --> 00:00:03,839 Let's now talk about loops which are easily one of the 2 00:00:03,849 --> 00:00:08,789 most important aspects of any kind of program or programming language. 3 00:00:09,090 --> 00:00:14,680 What exactly are they? Well, they are used typically to repeat a block of code 4 00:00:14,909 --> 00:00:16,629 multiple times 5 00:00:16,889 --> 00:00:20,950 until a specified condition is met or 6 00:00:21,139 --> 00:00:25,229 for a set number of iterations. 7 00:00:26,170 --> 00:00:33,849 In other words, a loop will run until all items in a sequence or list 8 00:00:34,259 --> 00:00:36,369 have been processed, 9 00:00:36,750 --> 00:00:37,650 or 10 00:00:37,750 --> 00:00:41,919 the loop will continue to run while a condition 11 00:00:42,119 --> 00:00:46,250 continues to be true or is satisfied. 12 00:00:46,439 --> 00:00:48,130 So basically, it's a loop, 13 00:00:48,139 --> 00:00:54,599 it'll keep going on and on and on and on until something eventually happens. 14 00:00:54,919 --> 00:00:55,529 Now, 15 00:00:56,099 --> 00:01:01,959 they are very important because they help us to automate repetitive tasks. 16 00:01:02,490 --> 00:01:06,830 So instead of us having to repeat the same task over and over again, 17 00:01:06,919 --> 00:01:11,099 we can just write a loop that will run that task force automatically. 18 00:01:11,209 --> 00:01:14,129 And of course, we use loops for building efficient programs 19 00:01:14,260 --> 00:01:18,559 that will adapt based on dynamic conditions. 20 00:01:18,989 --> 00:01:22,220 And when it comes to loops, there are two main types. 21 00:01:22,230 --> 00:01:24,360 The first one is going to be the for 22 00:01:24,610 --> 00:01:25,440 loop. 23 00:01:26,059 --> 00:01:27,830 Look at this, we have 24 00:01:27,970 --> 00:01:29,639 a list of 25 00:01:30,500 --> 00:01:33,069 129 OK numbers 26 00:01:33,930 --> 00:01:37,279 and we can write a very simple program that will say, hey, 27 00:01:37,529 --> 00:01:38,500 print 28 00:01:38,870 --> 00:01:45,080 each number out. So basically print 123456789. 29 00:01:45,580 --> 00:01:47,599 But instead of us having to write 30 00:01:48,010 --> 00:01:50,989 each individual line for each individual numbers, a 31 00:01:51,199 --> 00:01:55,559 print one second line, print two third line, print three, 32 00:01:55,569 --> 00:02:00,290 we can just write a loop that will go through all the numbers 33 00:02:00,300 --> 00:02:04,230 in this list and then print out those numbers until of course, 34 00:02:04,239 --> 00:02:07,900 the last number has been printed, which is nine. 35 00:02:08,059 --> 00:02:12,899 This will be the definition of an example of a for 36 00:02:13,080 --> 00:02:13,860 loop. 37 00:02:14,990 --> 00:02:16,000 The second one 38 00:02:16,110 --> 00:02:19,000 is the while 39 00:02:19,179 --> 00:02:19,429 loop. 40 00:02:19,929 --> 00:02:22,389 So say for example, the time is between 41 00:02:22,979 --> 00:02:24,190 12:01 p.m. 42 00:02:24,300 --> 00:02:25,889 and 4:59 p.m. 43 00:02:26,020 --> 00:02:31,740 So as long as the time of the day is between this range, say good 44 00:02:32,300 --> 00:02:36,830 notice the difference between the two structures. OK. 45 00:02:36,839 --> 00:02:37,509 The first one here, 46 00:02:37,520 --> 00:02:41,770 which is the fold loop already has like a predefined list or sequence. 47 00:02:42,250 --> 00:02:47,660 And as long as we have items in that particular list, then do something. 48 00:02:48,399 --> 00:02:51,149 The while loop is a little bit different because it says, OK, 49 00:02:51,229 --> 00:02:53,779 as long as this condition continues to be 50 00:02:53,789 --> 00:02:56,360 satisfied or this condition continues to be true. 51 00:02:56,419 --> 00:02:57,750 What is the condition here? 52 00:02:57,759 --> 00:03:04,740 As long as the time is between 12 to 1 pm and 4:59 p.m. Say good afternoon. 53 00:03:05,470 --> 00:03:05,929 Alright. 54 00:03:07,850 --> 00:03:08,660 In closing 55 00:03:09,539 --> 00:03:15,440 the loops must have an end point or will run forever. 56 00:03:15,750 --> 00:03:16,770 Basically, a 57 00:03:17,080 --> 00:03:20,440 loop can crush the entire program. 58 00:03:21,300 --> 00:03:23,029 Let's look at the example in here, 59 00:03:23,039 --> 00:03:28,059 what if the numbers in this list was number one to infinity, 60 00:03:28,220 --> 00:03:28,830 right? 61 00:03:29,020 --> 00:03:30,000 That means 62 00:03:30,210 --> 00:03:33,199 the program will continue to print the numbers over and 63 00:03:33,210 --> 00:03:36,300 over and over again and it will never stop, 64 00:03:36,309 --> 00:03:38,520 this will easily crash the program. 65 00:03:38,529 --> 00:03:40,389 And what if in this example right now 66 00:03:40,550 --> 00:03:43,649 there is no specified time period. 67 00:03:44,039 --> 00:03:46,759 What if the program just checks to see if it's doing 68 00:03:46,990 --> 00:03:50,210 the day or any time during the night, just say good afternoon. 69 00:03:50,220 --> 00:03:54,169 Well, it's always going to simply print out good afternoon, good afternoon, 70 00:03:54,179 --> 00:03:55,250 good afternoon, good afternoon. 71 00:03:55,259 --> 00:03:57,729 And it will eventually crash the program as well. So 72 00:03:58,070 --> 00:04:01,309 it is very, very important that whenever we're working with loops, 73 00:04:01,449 --> 00:04:05,210 there must always be a certain kind of condition 74 00:04:05,580 --> 00:04:10,690 that will eventually be satisfied for that loop to end. 75 00:04:10,889 --> 00:04:12,970 Otherwise it will crush the program. 76 00:04:13,199 --> 00:04:16,329 Thank you for watching the video. I will see you in the next class.