Break and continue, give you more control over your lupe's.
So far, you've used for loops and while loops to run code many times when you need to run code a specific
number of times, it's better to use a for loop.
And when it's not clear how many times you need to run a block of code, you have to use a while loop
because it's going to keep running your code as long as a condition holds true.
In this lesson, you're going to use the break and continue keywords inside of your loops and you're
going to see first hand how they can give us more control over them.
So the first thing you'll need to do is create a new class inside the Section five folder, make a new
file named Break and continue Java and inside the class, make sure it has the main method.
The continued keyword skips a single run you can use, continue to skip a run in the loop and continue
to the next one, I'm going to repeat this one more time.
The continued keyword skips run in the loop and it continues to the next one.
So inside Main, I'm going to write a for loop that prints the numbers zero to 10.
So the for loop is going to start with a counter that equals zero, equals zero.
The loop will keep running as long as I is smaller than or equal to 10.
And then I'll proceed to printing the counter every time the loop runs.
OK, I'll run my code.
And as you can simply expect, it prints the numbers zero to 10.
Now what if you want to get the odd numbers and only print the even numbers?
So like zero to four, six, eight and 10.
Inside the for loop, we need to check if the number is odd, if the number is odd, if the modulus
from the Vining the counter by two is not equal to zero, then skip the current run and continue to
the next one.
Let's say we reach a run with a counter equals three.
This condition is going to be true.
Continue is going to skip that run and move to the next one, not giving it the chance to print.
And ultimately, this is going to make sure that during every run where the counter is odd, the print
statement is going to get skipped.
Let's run our code.
And it prints all the even numbers, it skips every run that involves an odd number, remember that
Modulus calculates the remainder of a division.
When you divide an even number by two, the remainder is zero.
When you divide an odd number by two, the remainder is non-zero.
If the counter is zero, that's an even number, so the condition is false, bypassing the statement
and running print line.
Now, the counter is one that's an odd number, so the condition is true, the statement runs and continues,
skips the current run and moves to the next one.
Now, the counter is to that's an even number.
The condition is false, once more bypassing the statement and running print line.
Now, this animation is pretty long if you want to watch it, by all means.
But if you're comfortable with what we talked about so far, then feel free to skip ahead.
OK, let's talk about the break, key word break completely breaks the loop.
So what if I replaced the continue key word with break?
And as soon as the number is on, it simply breaks the loop.
First, we're starting at zero, that's an even number, so that condition is false, bypassing the
statements and running print line.
Now, the counter is one that's an odd number, so the condition is true, the if statement runs and
break essentially breaks the loop prematurely.
In this case, using break doesn't make any sense.
I'm in a place back to continue keyword and run my code.
And tonight, you have full control over your lupe's.
You know how to use for loops, they're uncowed a specific number of times and you know how to use while
loops to uncowed an unknown number of times.
In this lesson, you learn how to use break and continue inside your loops.
One continue executes it, skips the current and run in the loop and continues on to the next one.
And you use continue to skip a run if the cancer was odd.
Now, when break executes, break completely breaks the loop, and I think it's safe to say that you're
now an expert in loops.