This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Loops and the Continue Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loops and the Continue Statement" Watch "Loops and the Continue Statement" New topic

Loops and the Continue Statement

Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
I want to have a loop that adds a value to a group of variables in sequential order. However, I want it to skip some of the variables if a tested condition is true, and the resume where it left off. Can you do this with a 'break' statement and a 'continue' statement, or does the continue statement alway return the loop to where it started? (If I use a for loop with a counter variable, when I break the loop and then continue it, will it resume with the last value of the counter varaible.)
I hope this question makes some sense.
Thanks for any help.
The Sunburned Surveyor
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
A "continue" will keep you in the innermost loop and put you back at the top. A "break" will cause you to exit from the innermost loop. It sounds like you want to use "continue". However you might be able to accomplish the same thing by using an "if" statement. We'd need more details to determine what is the 'best' way of doing it.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
continue will continue the loop at the next iteration.
This is a pretty simple thing to practice with and discover.
The odd numbers are printed. If continue didn't continue at the next iteration, we'd have an infinite loop.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Loops and the Continue Statement
Similar Threads
initialization in for()'s
break statement.
wield labeled continue statement
for loop
clarification: break and continue