Anyway to go to top of a loop in JAVA?

OneOfTheseDays

Diamond Member
Jan 15, 2000
7,052
0
0
I want my program to go back to the beginning of the loop if something happens, instead of going down and executing the rest of the instructions of the loop. How can i do this? THe "break" command will simply exit me from the loop, and using the "continue" command will just execute the rest of the code below.
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
Put the rest of the code in an "if" block. Here's some pseudo code:

while (bla bla bla)
{
..........
..........

if ( !condition) // if the condition is NOT met, execute the remaining code. If the condition IS met, then skip this code.
{
execute remaining code....
}
}
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
What do you mean "continue" will execute the rest of the code below? "continue" should do exactly what you're looking for.
 

m0ti

Senior member
Jul 6, 2001
975
0
0
I think you mean this:


outerloop:
for (....) {

innerloop:
while() {

innermostloop:
do {
...
if ()
break outerloop;
elseif () continue innerloop;

}while();


}

You can label any loop structure (for, while, do, ...), and within the scope of the loop you can make a call to break <label> or continue <label>.