0
Why continue statement won't work in while and do while loops?
I'm using continue statement in while and dowhile loop (in java) , I'm using cmd as a compiler , for example, i take while loop and sets the condition to i<20 and initialize i to 0 and i++ (as usual), and takes an if statement and sets the condition i == 16 then continue statement should be execute, the basic purpose of continue statement is to skip that iteration, but i think it won't work well with while and do while loop, i don't know if the problem is in loop or in the compiler itself (cmd).
9 odpowiedzi
+ 7
Anmol Kumar Please,
Read the comments in the lessons as you can find great examples and explanations.👍
https://www.sololearn.com/learn/Java/2206/
+ 3
continue works in while and do while loops.
You must have made some mistake, but you have to show us the code, otherwise we're left to guessing.
+ 2
We have the 'Code Playground' here. You can put the part with your loop there, save it and copypaste the link here.
0
How can I show you the code, it's impossible to add image in this comment section.
0
If you can give your email id , i can mail you the image.
0
When i==16, it loops infinitely. After that Never reached to i++, always 'continue' executing. It does not exit loop, so then terminating execution.
Use i++ before if condition.. Like..
i++;
if(i==16)
continue;
0
Anmol Kumar
I guess this is what you are trying to do:
public class Program
{
public static void main(String[] args) {
int i=0;
while(i<20){
i++;
if(i==16)
continue;
System.out.println(i);
}
}
}
- 1
The condition you've placed already makes the while loop continue with some other code. Use a continue statement when there might be an error that will stop the process or in a situation where you want some other code not related to the loop to run after your looping condition is met