1 00:00:00,960 --> 00:00:04,470 Welcome back to Learn to Code and JavaScript in this lecture. 2 00:00:04,500 --> 00:00:05,970 We're going to talk about looping. 3 00:00:07,350 --> 00:00:13,260 So with conditionals, we made logical decisions about which path we're going to take with looping. 4 00:00:13,260 --> 00:00:16,060 We make decisions about repeating code deaths. 5 00:00:17,970 --> 00:00:21,810 So essentially, we're going to do something repeatedly while a condition is true. 6 00:00:23,960 --> 00:00:33,200 So some examples of how we do looping, there's a for loop, there's a while loop in a do while loop. 7 00:00:35,180 --> 00:00:42,500 So here's some examples for Loop is more complicated than the ones with the word wild in them because 8 00:00:42,500 --> 00:00:48,530 a for loop, the first of all, there's a couple of different variations of for loops, but the most 9 00:00:48,530 --> 00:00:52,760 traditional basic for loop is a four letter. 10 00:00:52,970 --> 00:01:00,620 A variable started a value continue until it's reached a value and then add something to it each time. 11 00:01:01,520 --> 00:01:03,890 So in this case we're saying let ay equals zero. 12 00:01:03,900 --> 00:01:10,340 So we're going to set a variable ay to be the value zero and then we're going to start the loop and 13 00:01:10,340 --> 00:01:12,350 we're going to start the loop of is less than ten. 14 00:01:13,460 --> 00:01:18,560 And then at the end of the iteration we're going to do something and then we're going to increase the 15 00:01:18,560 --> 00:01:24,620 value of it by one with the plus plus and then we'll go back to the beginning and try again as I still 16 00:01:24,620 --> 00:01:25,360 less than ten. 17 00:01:25,370 --> 00:01:25,750 Yes. 18 00:01:25,760 --> 00:01:29,390 OK, so we'll do something that will increase the value of EI. 19 00:01:29,900 --> 00:01:37,070 So that's the most basic traditional for loop you can just for a loop so you can have a for loop inside 20 00:01:37,070 --> 00:01:40,380 of a for loop and think of this as two dimensions. 21 00:01:41,060 --> 00:01:48,650 So if you're going through every square instead of a larger square, you might be going across the column 22 00:01:48,650 --> 00:01:50,900 squares and you might be going down the road squares. 23 00:01:52,000 --> 00:02:00,340 So think of a bingo card, if you're remember with that, where you have a board that's broken into 24 00:02:01,030 --> 00:02:08,320 five by five squares and so you go across the the five in the columns and then down each one clear across 25 00:02:08,320 --> 00:02:10,200 all five as an example. 26 00:02:11,320 --> 00:02:14,180 And one of the key words you can use here is to continue. 27 00:02:14,830 --> 00:02:18,160 So what the continue does is say at this condition is true. 28 00:02:18,160 --> 00:02:22,270 Just continue with the loop, go back to the beginning and start again, essentially. 29 00:02:23,470 --> 00:02:27,190 So if the condition is true, we're not going to do the second something. 30 00:02:30,800 --> 00:02:37,580 And in this example, with the if there's a break statement that says, hey, if the condition is true, 31 00:02:38,810 --> 00:02:39,800 break out of the loop. 32 00:02:40,400 --> 00:02:44,510 And incidentally, you don't have to use this in conjunction with the condition, you could just say 33 00:02:44,510 --> 00:02:50,620 break and it wouldn't do the rest of the world or continue and would always continue back to the beginning 34 00:02:50,630 --> 00:02:54,230 at that point, which is most commonly done with enough. 35 00:02:58,340 --> 00:03:05,450 Those are the type of variation on the for loop, which is a form of with which lets you iterate something 36 00:03:05,450 --> 00:03:06,420 such as an array. 37 00:03:07,400 --> 00:03:12,620 So if we have an array with two, four, six, eight, 10, and I said let a equals that array, I could 38 00:03:12,620 --> 00:03:14,450 say let the of a. 39 00:03:14,480 --> 00:03:18,290 And then this is going to take on all the values in the array. 40 00:03:19,070 --> 00:03:19,430 All right. 41 00:03:19,490 --> 00:03:21,580 Let's take a look at the while loop examples. 42 00:03:22,760 --> 00:03:28,080 So the basic while loop just checks the condition at the beginning like an F check, the condition in 43 00:03:28,090 --> 00:03:28,700 it is true. 44 00:03:28,700 --> 00:03:29,940 It's going to keep executing. 45 00:03:30,800 --> 00:03:33,650 So you'd better do something to make it false eventually. 46 00:03:34,220 --> 00:03:41,870 So in this case, I'm saying if execrated zero, continue the loop and then we'll subtract one from 47 00:03:41,870 --> 00:03:42,230 X. 48 00:03:42,800 --> 00:03:44,590 So that started out as ten. 49 00:03:45,350 --> 00:03:47,440 It's going to be ten, nine than eight. 50 00:03:47,450 --> 00:03:50,840 And then once it gets down to zero, it's going to stop executing. 51 00:03:53,410 --> 00:04:00,310 The doodler is like the wild loop, it checks a wild condition at the bottom instead of the top. 52 00:04:02,010 --> 00:04:10,740 So you always enter the loop on the while loop, if the if X is starting value was zero, you would 53 00:04:10,740 --> 00:04:11,880 never enter the loop. 54 00:04:12,630 --> 00:04:16,830 But on the do you always enter the loop for at least one path? 55 00:04:18,960 --> 00:04:22,770 So let's take a look in visual studio code as some examples of looping.