| Author |
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
Sheriff
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]
|
 |
 |
|
|
subject: Loops and the Continue Statement
|
|
|