Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loops and the Continue Statement

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic