This course explores the fundamental concepts and syntax associated with Control Flow and showcases these with some real-life projects.
- Learn about the three categories of control statements: sequential, selection, and repetition
- Manipulate control statements using the continue and break statements
- Learn about pseudo-random numbers and the Random class
- Beginner coders or anyone new to Java
- Experienced Java programmers who want to maintain their Java knowledge
- Developers looking to upskill for a project or career change
- College students and anyone else studying Java
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
In the previous lecture, we looked at repetition control statements and how we can use them to repeatedly execute the same block of code. In some cases, we might want to have our code end early or skip certain iterations. This is what the continue and break statements are for when used with loops. Let's get going. Create a new file in our Section3-Projects named ContinueBreak. So, let's go over here, 'New', 'Java Class', 'ContinueBreak' like that. And we will give it a main method: public static void main(String args). I'll put my little comment right there. So, let's use a while loop. We could, of course, use a do while loop or a for loop for this and the behavior would be approximately the same.
So, we're going to have a while loop. We'll set counter up to zero, or count, and then we'll say while zero or while the counter, rather, is less than 10 or count. We call it count less than 10 and then end while. We want to increment count so we don't get an infinite loop and it moves towards termination, making the loop continuation condition false. And then, we'll just print out the count. So, we'll do that. So, it's a System.out.println, print out count. In fact, I think what we're going to do, let's just do a print just to have some variety here and I'm going to a tab here. So, instead of printing one on each line, it'll put tabs in between and will probably wrap around at some point.
So, let's see what happens here. So, if we put an if statement in here and conditionally use continue, let's see what happens. So we'll put this, let's say, before I print out the count, I'm going to say if the count == 5, I want to continue. Let's run it and see if there's something unusual about what's going on. So, let's right click and go to 'Run' and let's see what we get. So, there we go. Now, this is interesting. So, we actually have an infinite loop here. So, there's a problem. So, I'm going to stop that and we actually have to make sure that we also increment count in here. Otherwise, we're going to end up with an infinite loop so that could be a very serious or significant problem.
Oops, that has to happen before the continue statement. So, that's one thing that could happen. Let's see what this does now. So, now that it's corrected a little bit, or we could put it after the continue at the bottom, or the count at the bottom. So, let's see what happens. There we go. Okay. That's more of the behavior I was looking for, but it was a good interesting little logic bug that popped up that I wasn't thinking about right away and that's good. See, even very experienced developers sometimes don't think of something right off the bat, and that's a good way to explore the different problems you can come across. Because what was happening is continue, which you should be able to see here now that we've got the count situation asserted properly.
You'll notice that the 5 is missing, and that's because it prints out the zero, prints out the one, the two, the three, the four. When it gets to five, it increments the count to six. But we're already in this statement and it goes to continue. What that does is it goes to the top of the loop right here, and then it just goes to the next, uses the next iteration, whatever happens in the next iteration. But the problem we had before when we didn't have count++ here is it says is count five. Okay, go back up to the top, but count still five. We didn't change it. So, it'll go counts five. That's less than 10, keep going, counts five. That's less than 10, keep going.
So, we ended up with an infinite loop. So, it's a little more of a complex way to have an infinite loop rather than just some little tiny simple thing. So, but it still is a pretty simple mistake. Now, you should be able to see that, again, the five is missing and that's because the continue statement causes the iteration in which it's currently executing to be skipped and the next iteration starts. So, what about the break statement. So, let's change our continue statement to break and rerun the application. Let's change this continue to a break statement. We've seen this with the switch statements for the selection control statements section.
But right here, our less on rather lecture. But right here what we have is the count being incremental. But that's not really going to matter right here because the break is going to have a different, slightly different behavior than it does or what we might expect with the switch statement. So, let's right click 'ContinueBreak', go to 'Run'. And in this case, it goes 0, 1, 2, 3, 4. It's not frozen because we can see that the program ended, and you can see that it did not get to five. It did not print five. What happens is as soon as the count got to five, it did increment to six, but that's really irrelevant for the break and then it broke out of the loop. So, it immediately breaks out of the loop when it got to five. It never got to 10. Now, are there better ways to do this? You would make the loop stop it five instead of using a break statement. But there are situations where breaking earlier using continue might help you. So, it's even more interesting. When the counts equal to five, we break out of the loop entirely. This works with for loops, do while loops, and regular while loops. So, now I have a small challenge for you. Can you figure out how to use the continue statement so that you loop through and only print the even integer zero through 10 exclusive, meaning we don't include 10?
That is, only integers divisible by two. As a hint, I want you to think about whether we have any operators that might help us determine if an integer is divisible by two.
So, pause the video and create a new Java file called EvenOnly, and come back when you think you've got the solution or if you need some help. How did that work out for you? That may have been a little challenging if you didn't remember that we have a really handy arithmetic operator to test for divisibility. Remember the modulus operator. This operator returns the remainder after a division problem. So, if we divide something by two and there's no remainder, that is zero, then it's even, correct. Let's code this together. So, we're going to create the EvenOnly file and we'll fill in the skeleton to start 'New' and the new Java class we'll call it EvenOnly. And right here, I'm going to go in here public static void main.
All right. So, now that we've got our skeleton, we're going to code a while loop and count from zero through 10 exclusive without worrying about skipping any values yet. So, that should be pretty familiar to what we just did, or similar to what we just did. So, it's familiar code less than 10. So, that's exclusive and count++. So, we've got that. We've got the end of the while loop right here. Here we go. And so, this is nearly identical to what we had before. And the difference though is that we want to be able to skip the odd numbers. So, we want to print out the even numbers. So, that's one way to think of it as skipping the odd numbers. So, here's where we add the magic. It's going to be an if statement. Note that we want to use continue.
So, we want to skip the odd numbers, thus, we want to skip if the remainder is not zero. If remainder is zero, that means it's even. So, we print it. Note that we must also make sure that we increment the counter variable in either case. Otherwise we're going to end up with an infinite loop like we did earlier. If the code never reaches count++, it's not going to change count. So, it never moves toward the termination of the loop, which we saw demonstrated earlier. So, right here, we're going to put the, if the count % 2, % not n is not equal to zero. So, if we have an odd number, we want to increment the count of course, and then do continue.
Now if you were using a for loop, you wouldn't have to keep track of this count++ here, you could just it would be in the header. So, it would happen automatically when you did a continue. Now we also, right down here, want to make sure that we do System.out.println, and this time, we'll do println, I guess, we'll do a count. There we go. So, let's run it and see what happens, 'EvenOnly', 'Run' it. And we get 0, 2, 4, 6, 8 and that's exactly what we expected. Nice. So, that's pretty cool. As a side note, a lot of developers try to avoid using break and continue because they are similar to an old statement from older languages that have survived called the goto statement.
The goto statement is considered to be a very bad statement that caused code to jump all over the place, creating what software developers call spaghetti code. Very disorganized, hard to follow, jumping all over the place. It could be really bad. An entire revolution and the invention of what we call the structured programming paradigm. And programming languages was invented largely to prevent spaghetti code. And this generally meant eliminating goto statements. So, some people avoid break and continue because they remind them of goto statements, but they aren't really that bad in my opinion. It is true that you can come up with different code to accomplish what you need in order to avoid break and continue.
For example, with the while loop, you could add two each time instead of one and then that would get you even values. But, sometimes they can be very, very helpful and developers still use them occasionally, and they're very handy. So, I'd say have at it. If it makes sense to use them, then do it. We learnt a ton so far. So, we're pretty much done with the major content of this section before we do some programming exercises. However, I would like to take a little detour and what we're going to do is we're going to discuss how to generate random numbers in the next lecture. I'll see you there.