1
00:00:09,030 --> 00:00:13,240
Hey, everyone, in this video, we're going to talk about break and continue.

2
00:00:13,500 --> 00:00:19,470
These statements are going to be used mainly with loops, and their purpose is to either interrupt the

3
00:00:19,470 --> 00:00:22,710
loop or just jump one iteration.

4
00:00:23,130 --> 00:00:30,290
So let's start with the break statement, which can be used to interrupt the loop or a switch statement.

5
00:00:30,300 --> 00:00:33,090
I haven't talked about the switch statement yet.

6
00:00:33,270 --> 00:00:36,750
If you want to know more, you can just click on this link that I left here.

7
00:00:36,900 --> 00:00:38,610
Maybe later in the course.

8
00:00:38,610 --> 00:00:42,000
I'm going to explain this with more details.

9
00:00:42,210 --> 00:00:45,530
But for now, let's just see the example using a loop.

10
00:00:45,930 --> 00:00:50,370
So going to Visual Studio, I'm just going to start a while loop here.

11
00:00:50,700 --> 00:00:52,620
So I'm going to create a variable.

12
00:00:52,620 --> 00:00:55,620
So far, X equals zero.

13
00:00:59,000 --> 00:01:06,740
So while X is less than 10, we are going to do something and what am I going to do here is just sending

14
00:01:06,740 --> 00:01:13,460
X to the console and then I'm going to increment the value of X. We don't have to go back to our Web

15
00:01:13,460 --> 00:01:15,320
page to know what's going to happen.

16
00:01:16,380 --> 00:01:20,740
We are just going to send Axe to the council until X is nine.

17
00:01:21,150 --> 00:01:21,600
All right.

18
00:01:21,600 --> 00:01:25,530
So what if we wanted to interrupt this loop earlier?

19
00:01:25,800 --> 00:01:31,020
If we don't want to change the condition here, we can just use a break statement.

20
00:01:31,350 --> 00:01:35,820
So I'm just going to start a conditional here.

21
00:01:37,310 --> 00:01:41,480
And I'm going to say that if X equals five.

22
00:01:42,980 --> 00:01:48,980
Then I just want to stop this loop, so I'm just going to use the brake statement.

23
00:01:50,170 --> 00:01:56,890
So no matter what is the condition for the loop, when our cold hits this brake statement, it is just

24
00:01:56,890 --> 00:01:58,400
going to go out of the loop.

25
00:01:58,810 --> 00:02:01,780
So going back to our page, refreshing it.

26
00:02:02,500 --> 00:02:03,640
We can see that now.

27
00:02:03,640 --> 00:02:04,990
We stopped at 4:00.

28
00:02:05,260 --> 00:02:12,280
This is because when we sent four to the console right after this, we incremented the value of X,

29
00:02:12,580 --> 00:02:13,860
which became five.

30
00:02:14,140 --> 00:02:17,730
So then we entered here and broke out of the loop.

31
00:02:18,070 --> 00:02:25,240
I know this sounds like a silly example, because after all, why didn't we just change it here instead

32
00:02:25,240 --> 00:02:26,920
of using the brake statement?

33
00:02:27,340 --> 00:02:31,480
But this was just an example of how you can interrupt a loop.

34
00:02:32,020 --> 00:02:36,610
There will be situations in which you are going to want to do this.

35
00:02:37,000 --> 00:02:39,670
So let's go now to the next example.

36
00:02:39,670 --> 00:02:41,790
The continue is a bit different.

37
00:02:42,040 --> 00:02:46,270
We are going to jump out of the loop, just like with the brake statement.

38
00:02:46,480 --> 00:02:50,080
But the difference is that we are not going to interrupt the loop.

39
00:02:50,110 --> 00:02:53,420
We are just going to jump the current iteration.

40
00:02:53,740 --> 00:03:01,150
So for this example, I'm going to write some code that is going to send to the console only the even

41
00:03:01,150 --> 00:03:03,600
numbers from zero to twenty.

42
00:03:03,610 --> 00:03:05,290
So I'm going to start a loop.

43
00:03:05,290 --> 00:03:11,240
And instead of sending all the numbers to the console, I'm just going to send the even numbers.

44
00:03:11,350 --> 00:03:19,270
So how can we do this before we start writing this, I just want to talk to you about the modulus operator,

45
00:03:19,270 --> 00:03:20,410
which is this one.

46
00:03:20,710 --> 00:03:26,330
This operator serves to return the remainder of a division.

47
00:03:26,590 --> 00:03:32,470
So if I divide four by two, I will have no remainder.

48
00:03:32,620 --> 00:03:36,830
The result is going to be two and the remainder is going to be zero.

49
00:03:37,030 --> 00:03:43,300
So if I try to do four, modulus two, we can see that the remainder is zero.

50
00:03:43,660 --> 00:03:46,720
But if I try to divide four by three.

51
00:03:48,220 --> 00:03:58,540
I'm going to have the remainder of one, if I tried to divide ten by three, I will have a remainder

52
00:03:58,540 --> 00:03:59,440
of one.

53
00:03:59,470 --> 00:04:02,890
So this is how the modulus operator works.

54
00:04:03,290 --> 00:04:13,270
So this is useful to identify even and odd numbers, because any even number that we do, Modulus two,

55
00:04:13,300 --> 00:04:15,130
is going to return in zero.

56
00:04:15,370 --> 00:04:19,420
So we can try 10, 12, 14, 16.

57
00:04:19,720 --> 00:04:27,370
This is always going to return zero because there's no remainder when we divide an even number by two.

58
00:04:27,610 --> 00:04:29,530
But any odd number.

59
00:04:30,010 --> 00:04:37,510
So let's try 11, it's going to have the result of one, because every odd number that we divide by

60
00:04:37,510 --> 00:04:41,380
two is always going to have a remainder of one.

61
00:04:41,410 --> 00:04:43,500
So this is how this operator works.

62
00:04:43,750 --> 00:04:49,940
So now that we know this, let's try to write a loop that's going to print only the even numbers.

63
00:04:50,380 --> 00:04:52,450
So going to Visual Studio Code.

64
00:04:56,450 --> 00:05:04,250
So I'm going to start with a variable called NUM, this is going to start at zero and then I'm just

65
00:05:04,250 --> 00:05:07,250
going to start a while loop.

66
00:05:09,380 --> 00:05:17,210
So while numb is less than or equal to 20, because I want to print all the even numbers all the way

67
00:05:17,210 --> 00:05:21,170
up to 20, so this is going to be the condition to enter the loop.

68
00:05:21,170 --> 00:05:23,960
And this time I'm going to do a bit differently.

69
00:05:24,230 --> 00:05:26,320
I'm going to explain this later.

70
00:05:26,630 --> 00:05:31,110
So I'm going to increment right at the beginning of the loop.

71
00:05:31,130 --> 00:05:37,490
So I'm just going to do num plus plus and then I'm just going to do console.

72
00:05:39,140 --> 00:05:40,190
That log.

73
00:05:41,190 --> 00:05:44,010
And I'm going to send the number.

74
00:05:46,150 --> 00:05:51,100
All right, so if we do this, we know what's going to happen, but let's try it out.

75
00:05:52,200 --> 00:05:59,220
We can see that we just sent all the numbers to the console all the way up to twenty, actually twenty

76
00:05:59,220 --> 00:05:59,580
one.

77
00:05:59,700 --> 00:06:07,610
So yeah, since I'm incrementing the value at the beginning of the loop, then we can just use number

78
00:06:07,770 --> 00:06:09,160
is less than 20.

79
00:06:09,180 --> 00:06:10,500
So let's try again.

80
00:06:12,060 --> 00:06:19,980
All right, so now it's stopping at 20, but we just want to print the even number, so how can we do

81
00:06:19,980 --> 00:06:20,460
this?

82
00:06:20,490 --> 00:06:22,470
There are two ways of doing this.

83
00:06:22,830 --> 00:06:27,000
So first, let's start by using the continued statement.

84
00:06:29,200 --> 00:06:33,760
So instead of just doing consolidated that log, let's make a test first.

85
00:06:33,910 --> 00:06:37,360
So let's start a conditional here.

86
00:06:38,910 --> 00:06:43,860
And we are going to test if num modulus to.

87
00:06:45,230 --> 00:06:46,040
It's not.

88
00:06:46,950 --> 00:06:47,580
Zero.

89
00:06:48,210 --> 00:06:54,820
So we know that every even number that we do, modula Stu, is going to return in zero.

90
00:06:54,990 --> 00:06:59,310
So if this doesn't happen, it means that this is an odd number.

91
00:06:59,550 --> 00:07:05,150
So if this is an odd number, we don't want to keep running this iteration.

92
00:07:05,430 --> 00:07:08,640
So we are just going to use the continue statement.

93
00:07:09,010 --> 00:07:14,220
So when the interpreter hits this line, it's just going to jump out of the loop.

94
00:07:14,220 --> 00:07:18,590
So it's not going to execute this line in any other line after this.

95
00:07:18,780 --> 00:07:23,490
So after to continue, it's just going to jump out of the loop and start again.

96
00:07:24,360 --> 00:07:29,310
When it hits the continue again, it jumps out of the loop and starts again.

97
00:07:29,340 --> 00:07:31,900
So this is how the continuous statement works.

98
00:07:32,640 --> 00:07:34,170
So if we do this.

99
00:07:35,450 --> 00:07:41,490
And refresh the page, we can see that now we only printed the even numbers.

100
00:07:41,540 --> 00:07:47,660
Of course, this is not the only way of doing this, because after all, we could have done it like

101
00:07:47,660 --> 00:07:55,020
this, just getting the console that long and putting it inside here and then doing the opposite.

102
00:07:55,280 --> 00:07:59,500
So if num modulus two equals zero, then we do console that log.

103
00:07:59,630 --> 00:08:02,120
If not, we just do nothing.

104
00:08:02,660 --> 00:08:07,220
So if we refresh the page, we can see that now we have the same result.

105
00:08:07,550 --> 00:08:13,160
So like I always say, there's never just one way of solving a problem.

106
00:08:13,170 --> 00:08:15,080
There's never just one solution.

107
00:08:15,230 --> 00:08:20,480
But I thought it would be interesting to show you how to solve this using the continuous statement.

108
00:08:20,490 --> 00:08:22,460
So you know how this works.

109
00:08:22,910 --> 00:08:23,360
All right.

110
00:08:23,370 --> 00:08:27,440
So I just wanted to quickly show you these two examples.

111
00:08:27,560 --> 00:08:30,830
Maybe we're going to see these statements later in the course.

112
00:08:30,830 --> 00:08:32,360
But for now, that was out.

113
00:08:32,370 --> 00:08:34,420
So I'll wait for you in the next video.
