1
00:00:01,316 --> 00:00:03,262
Let's take a look at two other statements

2
00:00:03,262 --> 00:00:06,346
that you might use in combination with

3
00:00:06,346 --> 00:00:08,642
looping. First one is break. Now, we've

4
00:00:08,642 --> 00:00:11,481
seen break before. That was in the context

5
00:00:11,481 --> 00:00:13,885
of the switch statement. In this case,

6
00:00:13,885 --> 00:00:16,612
what break does is leaves a loop early.

7
00:00:16,612 --> 00:00:19,962
The other statement you might use is

8
00:00:19,962 --> 00:00:22,918
continue, and continue will let you go

9
00:00:22,918 --> 00:00:25,053
back up to the top of the loop and

10
00:00:25,053 --> 00:00:27,542
continue with the next iteration,

11
00:00:27,542 --> 00:00:32,290
bypassing any code below the continue.

12
00:00:32,290 --> 00:00:34,259
Let's take a look at these two demos of

13
00:00:34,259 --> 00:00:40,978
break and continue. Our first example is a

14
00:00:40,978 --> 00:00:45,665
break. I still have the same products

15
00:00:45,665 --> 00:00:49,262
array. But down here now you can see the

16
00:00:49,262 --> 00:00:51,796
function breakSample. I'm going to now use

17
00:00:51,796 --> 00:00:54,013
the for loop, and I'm going to do a for/of

18
00:00:54,013 --> 00:00:57,011
because I'm iterating over an array of

19
00:00:57,011 --> 00:00:59,071
product objects. What I'm going to do is,

20
00:00:59,071 --> 00:01:01,624
I'm going to check inside of the for/of.

21
00:01:01,624 --> 00:01:03,056
I'm going to do an if statement to check

22
00:01:03,056 --> 00:01:05,806
to see if the standardCost is less than

23
00:01:05,806 --> 00:01:09,376
20. If it is, I will break out of the

24
00:01:09,376 --> 00:01:12,146
loop, which means it skips any other

25
00:01:12,146 --> 00:01:15,944
statements below the break. On line 47,

26
00:01:15,944 --> 00:01:17,376
what we're going to do each time through

27
00:01:17,376 --> 00:01:19,597
the loop is do a console.log on the

28
00:01:19,597 --> 00:01:24,106
JSON.stringify of the item. But once the

29
00:01:24,106 --> 00:01:26,763
standardCost is less than 20, we will

30
00:01:26,763 --> 00:01:29,649
break out of the loop. Let's take a look

31
00:01:29,649 --> 00:01:32,821
up here, and we see that the first

32
00:01:32,821 --> 00:01:37,582
standardCost on line 20 is 1059. But then

33
00:01:37,582 --> 00:01:41,020
on line 28, the standardCost is 13.08. So

34
00:01:41,020 --> 00:01:46,060
it should skip the last two items in the

35
00:01:46,060 --> 00:01:50,277
array. Let's open this up in our default

36
00:01:50,277 --> 00:01:53,739
browser, bring up the F12 tools, and we

37
00:01:53,739 --> 00:01:57,403
see that indeed we do just get the first

38
00:01:57,403 --> 00:02:02,565
object and then the rest of the loop is

39
00:02:02,565 --> 00:02:06,225
broken out of. The continue statement in

40
00:02:06,225 --> 00:02:09,494
our continueSample, if we take a look at

41
00:02:09,494 --> 00:02:13,177
this function, this one is kind of the

42
00:02:13,177 --> 00:02:15,666
opposite of break. What this one will do

43
00:02:15,666 --> 00:02:18,641
is go back up to the top of the loop and

44
00:02:18,641 --> 00:02:21,862
continue with the iteration but still skip

45
00:02:21,862 --> 00:02:25,221
any lines below it. So just like the break

46
00:02:25,221 --> 00:02:26,911
skipped any lines, this one also skips

47
00:02:26,911 --> 00:02:28,504
them, but the difference is, it goes back

48
00:02:28,504 --> 00:02:31,554
to the top of the loop and continues. So

49
00:02:31,554 --> 00:02:33,229
on this one, inside of our for loop, we

50
00:02:33,229 --> 00:02:35,482
have an if statement that checks to see if

51
00:02:35,482 --> 00:02:38,910
the standardCost of that product object is

52
00:02:38,910 --> 00:02:41,193
greater than 1000, and if it does, it says

53
00:02:41,193 --> 00:02:43,409
continue. So the first one is definitely

54
00:02:43,409 --> 00:02:46,864
over 1000, so it will not perform line 47.

55
00:02:46,864 --> 00:02:49,954
Instead it will go to the top, get the

56
00:02:49,954 --> 00:02:52,714
next item, and then continue. Let's go

57
00:02:52,714 --> 00:02:54,426
ahead and open this up in our default

58
00:02:54,426 --> 00:02:57,387
browser. We'll bring up our F12 tools,

59
00:02:57,387 --> 00:03:00,919
click on continue, and we should see only

60
00:03:00,919 --> 00:03:05,000
two products get reported. The first one is skipped.

