1
00:00:00,300 --> 00:00:05,310
Hey, in this lesson, we're going to be covering the JavaScript break statement and what the brake

2
00:00:05,310 --> 00:00:12,750
statement does is it terminates a loop or a switch or a label, and it allows you to transfer control

3
00:00:12,750 --> 00:00:16,180
from that statement and allows you to terminate the statement.

4
00:00:16,210 --> 00:00:17,690
So let me give you an example of it.

5
00:00:18,060 --> 00:00:22,790
So an example of having a break within a loop.

6
00:00:23,160 --> 00:00:32,370
So let's set the value of X to be equal to zero and we're going to do a while and continue to loop while

7
00:00:32,370 --> 00:00:34,740
X is less than 10.

8
00:00:37,030 --> 00:00:45,730
And then we do need to increment X, so increment X plus one, and let's put the result here in the

9
00:00:45,730 --> 00:00:50,550
console and we'll console whatever the value of access.

10
00:00:50,950 --> 00:00:51,950
So put that.

11
00:00:52,480 --> 00:00:54,370
So let's see what happens in the console.

12
00:00:54,370 --> 00:00:59,320
So we get zero one, two, three, four, five, six, seven, eight, nine as we're starting with zero

13
00:00:59,320 --> 00:01:03,690
and we're continuing the loop while it's less than 10.

14
00:01:04,360 --> 00:01:12,160
So how about we add in a break so we'll add another condition and we'll check to see if X is equal to

15
00:01:12,160 --> 00:01:12,640
five.

16
00:01:12,970 --> 00:01:17,640
And we could do an absolute because making sure that the data type is the same as well.

17
00:01:18,160 --> 00:01:20,610
And then let's add in a break here.

18
00:01:21,010 --> 00:01:30,640
So it's going to break if X is equal to five and then let's output the value of X in the console after

19
00:01:30,640 --> 00:01:32,360
the statement finishes running.

20
00:01:32,920 --> 00:01:34,700
So after that condition finishes running.

21
00:01:35,050 --> 00:01:38,050
So what do you think the value of X will be at this point?

22
00:01:38,230 --> 00:01:42,220
And how much how many Xs are we going to see within the console?

23
00:01:42,580 --> 00:01:44,800
So let's check it out and run it.

24
00:01:44,980 --> 00:01:51,250
And if you said that at five, so once X is equal to five, it's going to stop the condition.

25
00:01:51,250 --> 00:01:52,040
You're correct.

26
00:01:52,300 --> 00:02:00,700
So what happened here is it ran through and it incremented the value of X, and then at this point when

27
00:02:00,700 --> 00:02:04,390
X was equal to five, then it broke that.

28
00:02:04,570 --> 00:02:08,230
And once it breaks it, it doesn't finish writing it out into the console.

29
00:02:08,560 --> 00:02:15,130
So that's why we don't see the value of X being five in line twenty two.

30
00:02:15,310 --> 00:02:22,300
But we do see it in line twenty six because that's the next statement that runs after the break.

31
00:02:22,630 --> 00:02:30,760
So it runs through once X equals five, it breaks the loop and these two values don't.

32
00:02:30,760 --> 00:02:38,050
These two statements don't get run or executed and we move on to the following line line twenty six

33
00:02:38,530 --> 00:02:41,350
and I'll also do one for continue.

34
00:02:42,370 --> 00:02:47,380
So let's check out and see if X is equal to three.

35
00:02:48,040 --> 00:02:51,790
And if it is, then what we'll do is we're going to continue.

36
00:02:53,480 --> 00:02:58,580
And what continue does is it's a keyword that allows us to.

37
00:02:59,970 --> 00:03:05,910
Break one iteration of the statement, so we'll see what happens within the console and it's still going

38
00:03:05,910 --> 00:03:07,210
to be breaking at five.

39
00:03:07,250 --> 00:03:11,520
How about we change that to eight so we get more iterations of the console message?

40
00:03:11,850 --> 00:03:15,600
So what do you think the output is going to be and what do you think is going to we're going to see

41
00:03:15,600 --> 00:03:16,350
in the console?

42
00:03:16,530 --> 00:03:21,600
Remember, we're breaking we've got to continue if X is equal to three.

43
00:03:22,020 --> 00:03:23,040
So let's see what happens.

44
00:03:23,430 --> 00:03:26,970
So what happened here is we ran zero one two.

45
00:03:27,120 --> 00:03:29,160
So it's constantly still spinning.

46
00:03:29,160 --> 00:03:32,160
And that's because we've entered into an infinite loop.

47
00:03:32,310 --> 00:03:34,350
So the browser is not able to handle that.

48
00:03:34,620 --> 00:03:42,480
So effectively, we shouldn't be using the continue within the while loop because it's causing an error

49
00:03:42,480 --> 00:03:42,660
there.

50
00:03:42,660 --> 00:03:44,340
It's causing it to.

51
00:03:45,990 --> 00:03:49,450
Create an infinite loop on the continuum.

52
00:03:49,770 --> 00:03:57,090
So let's save that and we'll refresh it or close it and open up a new page and we're going to try that

53
00:03:57,090 --> 00:04:01,430
within the for loop and I'll open up the dev console.

54
00:04:01,440 --> 00:04:03,650
So we've got the console there once again.

55
00:04:04,230 --> 00:04:08,040
So we're not going to keep the continue loop in the while.

56
00:04:08,350 --> 00:04:17,130
Let's try this with for loop so that I equals zero to the same format and then we'll do four.

57
00:04:18,670 --> 00:04:29,230
I equals zero, and then we're going to continue our eye is less than 10 and then also increment I by

58
00:04:29,230 --> 00:04:31,240
one, so do it within a for loop.

59
00:04:31,480 --> 00:04:38,950
And actually, I'm going to take the value of AI and keep it within the for loop so that as we loop

60
00:04:38,950 --> 00:04:44,900
through will console log whatever the value of AI is and we'll see what that looks like.

61
00:04:44,950 --> 00:04:50,620
So refresh and this is the value of I outlined 19 that's being output.

62
00:04:51,490 --> 00:05:00,670
So now let's check out what happens to if we have a continue and if I equals three then we continue.

63
00:05:00,880 --> 00:05:04,530
So what do you think Continuos going to do here within the for loop?

64
00:05:04,960 --> 00:05:05,920
Let's refresh.

65
00:05:06,310 --> 00:05:10,950
And what happened here is we've got zero one two and we've skipped three.

66
00:05:11,170 --> 00:05:13,570
So that continue within the for loop.

67
00:05:13,750 --> 00:05:18,730
So it didn't create an infinite loop, but it allowed us to skip the one iteration.

68
00:05:18,970 --> 00:05:20,670
And also let's try break.

69
00:05:21,280 --> 00:05:24,940
So we'll check to see if I is equal to eight.

70
00:05:25,330 --> 00:05:27,500
And if it is, then we'll run a break.

71
00:05:28,120 --> 00:05:31,660
So what do you think is going to happen here and what do you think the output in the console is going

72
00:05:31,660 --> 00:05:32,010
to be?

73
00:05:32,350 --> 00:05:38,740
I'm going to come into these values for the console so that we only have the for loop sitting within

74
00:05:38,740 --> 00:05:39,370
the console.

75
00:05:39,610 --> 00:05:41,770
What do you think is going to be output in the console?

76
00:05:41,890 --> 00:05:43,680
Remember, AI is starting at zero.

77
00:05:43,840 --> 00:05:44,770
Let's refresh.

78
00:05:44,950 --> 00:05:51,520
We've got zero one to notice three is missing because we did the continue four, five, six, seven,

79
00:05:51,670 --> 00:05:55,030
and then when it hits eight, it breaks the for loop.

80
00:05:55,840 --> 00:05:59,830
So that's how you can use for and continue within your JavaScript code.

81
00:05:59,830 --> 00:06:00,460
So try to.
