Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,640 --> 00:00:03,130
Break and continue, give you more control over your lupe's.
2
00:00:04,110 --> 00:00:09,240
So far, you've used for loops and while loops to run code many times when you need to run code a specific
3
00:00:09,240 --> 00:00:11,580
number of times, it's better to use a for loop.
4
00:00:11,940 --> 00:00:16,680
And when it's not clear how many times you need to run a block of code, you have to use a while loop
5
00:00:16,980 --> 00:00:20,220
because it's going to keep running your code as long as a condition holds true.
6
00:00:24,320 --> 00:00:28,520
In this lesson, you're going to use the break and continue keywords inside of your loops and you're
7
00:00:28,520 --> 00:00:31,540
going to see first hand how they can give us more control over them.
8
00:00:33,580 --> 00:00:38,500
So the first thing you'll need to do is create a new class inside the Section five folder, make a new
9
00:00:38,500 --> 00:00:44,050
file named Break and continue Java and inside the class, make sure it has the main method.
10
00:00:49,620 --> 00:00:55,500
The continued keyword skips a single run you can use, continue to skip a run in the loop and continue
11
00:00:55,500 --> 00:00:58,410
to the next one, I'm going to repeat this one more time.
12
00:00:58,890 --> 00:01:02,880
The continued keyword skips run in the loop and it continues to the next one.
13
00:01:06,300 --> 00:01:10,480
So inside Main, I'm going to write a for loop that prints the numbers zero to 10.
14
00:01:11,760 --> 00:01:15,990
So the for loop is going to start with a counter that equals zero, equals zero.
15
00:01:18,410 --> 00:01:22,250
The loop will keep running as long as I is smaller than or equal to 10.
16
00:01:30,180 --> 00:01:34,020
And then I'll proceed to printing the counter every time the loop runs.
17
00:01:37,290 --> 00:01:38,610
OK, I'll run my code.
18
00:01:42,830 --> 00:01:46,500
And as you can simply expect, it prints the numbers zero to 10.
19
00:01:47,150 --> 00:01:50,380
Now what if you want to get the odd numbers and only print the even numbers?
20
00:01:50,390 --> 00:01:53,750
So like zero to four, six, eight and 10.
21
00:01:56,820 --> 00:02:02,220
Inside the for loop, we need to check if the number is odd, if the number is odd, if the modulus
22
00:02:02,550 --> 00:02:09,090
from the Vining the counter by two is not equal to zero, then skip the current run and continue to
23
00:02:09,090 --> 00:02:09,990
the next one.
24
00:02:15,220 --> 00:02:17,810
Let's say we reach a run with a counter equals three.
25
00:02:18,070 --> 00:02:19,660
This condition is going to be true.
26
00:02:19,870 --> 00:02:24,960
Continue is going to skip that run and move to the next one, not giving it the chance to print.
27
00:02:25,480 --> 00:02:30,130
And ultimately, this is going to make sure that during every run where the counter is odd, the print
28
00:02:30,130 --> 00:02:31,660
statement is going to get skipped.
29
00:02:33,690 --> 00:02:34,740
Let's run our code.
30
00:02:45,530 --> 00:02:50,900
And it prints all the even numbers, it skips every run that involves an odd number, remember that
31
00:02:50,900 --> 00:02:53,150
Modulus calculates the remainder of a division.
32
00:02:53,540 --> 00:02:56,780
When you divide an even number by two, the remainder is zero.
33
00:02:58,660 --> 00:03:01,630
When you divide an odd number by two, the remainder is non-zero.
34
00:03:09,240 --> 00:03:14,610
If the counter is zero, that's an even number, so the condition is false, bypassing the statement
35
00:03:14,610 --> 00:03:15,840
and running print line.
36
00:03:17,640 --> 00:03:24,810
Now, the counter is one that's an odd number, so the condition is true, the statement runs and continues,
37
00:03:24,810 --> 00:03:27,810
skips the current run and moves to the next one.
38
00:03:28,530 --> 00:03:31,290
Now, the counter is to that's an even number.
39
00:03:31,290 --> 00:03:35,250
The condition is false, once more bypassing the statement and running print line.
40
00:03:35,910 --> 00:03:39,600
Now, this animation is pretty long if you want to watch it, by all means.
41
00:03:39,600 --> 00:03:43,470
But if you're comfortable with what we talked about so far, then feel free to skip ahead.
42
00:04:28,710 --> 00:04:32,830
OK, let's talk about the break, key word break completely breaks the loop.
43
00:04:33,240 --> 00:04:36,000
So what if I replaced the continue key word with break?
44
00:04:43,820 --> 00:04:46,960
And as soon as the number is on, it simply breaks the loop.
45
00:04:49,110 --> 00:04:53,820
First, we're starting at zero, that's an even number, so that condition is false, bypassing the
46
00:04:53,940 --> 00:04:56,070
statements and running print line.
47
00:04:58,040 --> 00:05:03,500
Now, the counter is one that's an odd number, so the condition is true, the if statement runs and
48
00:05:03,500 --> 00:05:06,290
break essentially breaks the loop prematurely.
49
00:05:07,190 --> 00:05:09,460
In this case, using break doesn't make any sense.
50
00:05:09,500 --> 00:05:12,830
I'm in a place back to continue keyword and run my code.
51
00:05:18,760 --> 00:05:20,860
And tonight, you have full control over your lupe's.
52
00:05:23,580 --> 00:05:27,930
You know how to use for loops, they're uncowed a specific number of times and you know how to use while
53
00:05:27,930 --> 00:05:30,540
loops to uncowed an unknown number of times.
54
00:05:30,780 --> 00:05:34,320
In this lesson, you learn how to use break and continue inside your loops.
55
00:05:37,010 --> 00:05:42,470
One continue executes it, skips the current and run in the loop and continues on to the next one.
56
00:05:43,480 --> 00:05:46,660
And you use continue to skip a run if the cancer was odd.
57
00:05:51,990 --> 00:05:57,660
Now, when break executes, break completely breaks the loop, and I think it's safe to say that you're
58
00:05:57,660 --> 00:05:59,250
now an expert in loops.
6225
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.