Hi, let’s explore the topic of the Java Continue Statement. You can learn the material either in video format or in a more detailed text version with me below.
Java Continue
The continue statement in Java is commonly used for the following cases.- It skips the following statements and moves to the next iteration in the for loop.
- Continue in while loops hop the following statements and jump to the conditional statement.
Syntax
continue;
Example
public class Driver2 {
public static void main(String[] args) {
// Testing continue statement in while loop
System.out.println("Test Continue in While loop");
int i = 0;
while (i < 5) {
if (i == 2) {
i++;
continue;
}
System.out.println(i++);
}
}
}
Output
Test Continue in While loop
0
1
3
4
Differences between continue and break
So, continue what’s it about? Imagine you’ve got a loop going through numbers, say 1 to 10, and you only care about the even ones. When you hit an odd number, you don’t want to shut everything down; you just want to shrug it off and move to the next one. That’s continue in action. It’s like telling Java, “Eh, skip this one, let’s keep going.” I’ve used it tons of times like when I’m processing a list and need to ignore stuff that doesn’t fit the bill. It’s quick, clean, and keeps the loop rolling. Then there’s break, which is a whole different vibe. Say you’re digging through that same list, but now you’re hunting for one specific number like 5. The second you find it, you’re done. No point in wasting time on the rest, right? Break is your out. It doesn’t mess around it yanks you straight out of the loop, and your program moves on. I’ve leaned on it when I’m searching for something and don’t need to keep looking once I’ve got it. It’s like hitting the eject button when you’re done with the ride. Here’s the deal: continue keeps you in the game but lets you skip a turn, while break says, “I’m outta here.” I’ve seen new coders mix them up, but it’s simple once you think about it. Need to dodge something without stopping? Continue. Found what you need and want to bail? Break. Next time you’re stuck in a loop, just picture that treadmill do you want to hop over a step or jump off entirely? That’s the trick to nailing it.Break and Continue in While Loop
That’s where break and continue come in handy. These two keywords are like your secret weapons for controlling a loop, and as someone who’s been coding for a while, I can tell you they’re a game-changer once you get them down. Let’s kick off with continue. Say you’re counting down from 10 in a while loop, and you only want to print the even numbers. When you hit an odd one, you don’t want to quit you just want to skip it and keep going. That’s continue doing its thing. It tells Java, “Skip this round, back to the top!” Here’s a quick look at how it works:int num = 10;
while (num > 0) {
if (num % 2 != 0) { // Odd number? Skip it!
num--;
continue;
}
System.out.println(num);
num--;
}
Run that, and you’ll see 10, 8, 6, 4, 2—no odd numbers in sight. I’ve used continue like this when I’m sifting through data and need to ignore stuff that doesn’t fit—like skipping blank lines in a file.
Now, break is a whole different vibe. Imagine you’re still counting down, but you’re waiting for the number 5. Once you hit it, you’re done—no need to keep going. Break is your ticket out. It doesn’t just skip; it busts you out of the loop completely. Check this out:
int num = 10;
while (num > 0) {
if (num == 5) { // Hit 5? We’re outta here!
break;
}
System.out.println(num);
num--;
}
This prints 10, 9, 8, 7, 6, and then stops cold at 5. I’ve leaned on break when I’m hunting for something specific—like scanning a list for a match and bailing once I find it.
Here’s the deal: continue keeps the loop alive but lets you dodge a step, while break says, “I’m done, see ya!” I’ve used them together too—like in a loop where I skip zeros with continue and stop at a negative with break. Play with these in your own code, tweak the conditions, and you’ll see how they can save you from a ton of extra work.More reading: |
---|
GO TO FULL VERSION