1
00:00:00,960 --> 00:00:07,110
This lesson is going to be all about loops and iteration using JavaScript in order to do a for loop,

2
00:00:07,110 --> 00:00:11,940
do while loop while loop and break and continue are that are available within the loops.

3
00:00:12,210 --> 00:00:18,860
So what JavaScript loops do is they allow you to run multiple iterations and you can set the parameters.

4
00:00:19,020 --> 00:00:21,720
One thing to avoid with loops is infinite loop.

5
00:00:21,720 --> 00:00:26,640
So you don't want them to be able to loop forever that will cross the browser.

6
00:00:26,790 --> 00:00:29,910
So always make sure that you have a way out of your loop.

7
00:00:30,060 --> 00:00:36,180
And that's the purpose of a loop, is to constantly increment values or numbers and then find a way

8
00:00:36,180 --> 00:00:36,870
out of the loop.

9
00:00:36,900 --> 00:00:40,110
So once a condition is met, so for instance, this is a for loop.

10
00:00:40,140 --> 00:00:45,210
We've got a counter while the counter is less than five, it will continue.

11
00:00:45,330 --> 00:00:48,800
And then once that condition is no longer true, the loop will break.

12
00:00:48,810 --> 00:00:50,440
And this is true for all the loops.

13
00:00:50,460 --> 00:00:55,770
So as you're iterating through the content of a loop, you also have to option to break the loop.

14
00:00:55,780 --> 00:01:02,070
So you have ability to set a condition to break out of the loop so the break will stop the loop.

15
00:01:02,280 --> 00:01:04,950
There's also one that's called continue true.

16
00:01:04,950 --> 00:01:06,030
And then we continue.

17
00:01:06,210 --> 00:01:12,210
Although this one is not commonly used, it is available, but it's not advice because you might run

18
00:01:12,210 --> 00:01:13,840
into some problems with testing.

19
00:01:13,860 --> 00:01:20,130
So there is a challenge for this lesson and that's to create a loop of a counter using all three different

20
00:01:20,130 --> 00:01:21,390
iteration options.

21
00:01:21,390 --> 00:01:26,460
So you've got the for loop, we've got the while loop and as well the do while loop.

22
00:01:26,700 --> 00:01:28,240
There's an example of them.

23
00:01:28,350 --> 00:01:30,720
So these are all going to output the same thing.

24
00:01:30,840 --> 00:01:32,640
So each loop has a starting point.

25
00:01:32,670 --> 00:01:38,250
So setting the counter to zero where looping while the counter is less than five.

26
00:01:38,490 --> 00:01:41,400
And we also need a way to increase the counter.

27
00:01:41,580 --> 00:01:44,100
So the counter is incrementing by one.

28
00:01:44,250 --> 00:01:46,570
So this gives us our way out of the loop.

29
00:01:46,680 --> 00:01:53,190
So we've got our starting point as well within the while loop or setting X to zero while X is less than

30
00:01:53,190 --> 00:01:53,790
five.

31
00:01:54,030 --> 00:01:58,220
And then we're incrementing X within the while as well.

32
00:01:58,230 --> 00:02:06,390
We've got the do while loop, so we're checking to we're setting the value of it to zero where incrementing

33
00:02:06,390 --> 00:02:12,540
I with one and then we're doing this while I is less than five.

34
00:02:12,720 --> 00:02:15,400
So first let's create the most common loop.

35
00:02:15,570 --> 00:02:19,130
So this is the for loop and this is the one that you're going to encounter the most.

36
00:02:19,440 --> 00:02:21,890
So setting up our counter at zero.

37
00:02:21,900 --> 00:02:23,820
This is our starting point for the counter.

38
00:02:24,070 --> 00:02:26,620
And then next is the condition that we want to meet.

39
00:02:26,970 --> 00:02:34,160
So we want to loop this while counter is less than five and then we need a way out.

40
00:02:34,170 --> 00:02:35,550
So we need something different.

41
00:02:35,700 --> 00:02:37,240
We need a counter updating.

42
00:02:37,260 --> 00:02:41,190
So that's where we just got that counter updating, incrementing by one.

43
00:02:41,310 --> 00:02:43,230
And this will give us our way out.

44
00:02:43,230 --> 00:02:49,190
And I'll just write for Loop and then also add in the value of counter just after that.

45
00:02:49,500 --> 00:02:52,890
So take a look in the console and we see it starts at zero.

46
00:02:52,900 --> 00:02:54,290
So this is our starting value.

47
00:02:54,540 --> 00:02:56,520
We can start it up one as well.

48
00:02:56,880 --> 00:03:03,710
So that will start at one, two, three, four, and then we can loop while counter is less than six.

49
00:03:03,720 --> 00:03:06,410
So that means that it's going to break after five.

50
00:03:06,930 --> 00:03:08,250
So that was one example.

51
00:03:08,580 --> 00:03:12,810
The other example is this is while loop.

52
00:03:12,990 --> 00:03:14,900
So let's set our starting points.

53
00:03:14,900 --> 00:03:21,000
So using X as our starting point, we're going to loop while X is less than five.

54
00:03:21,210 --> 00:03:23,550
And then this is where the blockquote goes.

55
00:03:23,710 --> 00:03:29,040
Always make sure that your incrementing X because this is again are way out of the loop and this is

56
00:03:29,040 --> 00:03:30,450
our while loop.

57
00:03:30,450 --> 00:03:32,790
And then we can see the value of X.

58
00:03:33,150 --> 00:03:38,010
And depending on what we're trying to accomplish with your coding, you may choose you may prefer to

59
00:03:38,010 --> 00:03:40,230
use one looping method over the other.

60
00:03:40,440 --> 00:03:46,230
And also keep in mind that wherever we're producing the X increment, that's going to affect the value

61
00:03:46,230 --> 00:03:48,440
of X at the time we're running the code.

62
00:03:48,660 --> 00:03:52,850
So for placing it before or after, it will make a difference within the output.

63
00:03:53,310 --> 00:03:58,500
And then lastly, let's do one last loop and this is the do while loop.

64
00:03:58,920 --> 00:04:03,360
So this is very similar to the while loop and there is actually one important difference.

65
00:04:04,080 --> 00:04:08,250
So let's set it up and we'll run through it and then we'll see what the difference there is.

66
00:04:08,550 --> 00:04:09,990
So within the console log.

67
00:04:10,000 --> 00:04:14,880
So this is the do while and adding in the value of ice so that we can see it.

68
00:04:14,880 --> 00:04:20,030
And then the condition that's going to break this loop is while AI is less than five.

69
00:04:20,160 --> 00:04:21,060
So let's try that.

70
00:04:21,060 --> 00:04:28,860
And you can see that the result is the same thing where if we do the increment of AI afterwards, it's

71
00:04:28,860 --> 00:04:31,170
going to work the same way as the while loop.

72
00:04:31,380 --> 00:04:38,370
So the difference here is that if the starting values for the while loop are ten and for the do while

73
00:04:38,370 --> 00:04:41,580
loop R ten, you're going to see one interesting difference.

74
00:04:41,730 --> 00:04:50,220
And that means that the do while loop always runs at least one time, whereas the while loop, because

75
00:04:50,220 --> 00:04:55,850
this condition isn't met, it never runs this block of code because the value of X is ten.

76
00:04:56,100 --> 00:04:59,610
So if you need to run it at least one time.

77
00:04:59,930 --> 00:05:04,880
You can use a do while loop, otherwise you could just use a while loop, or if you prefer, you can

78
00:05:04,880 --> 00:05:07,430
use a for loop, so do try out all three.
