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).

30th Dec 2019, 5:26 PM
Anmol Kumar
Anmol Kumar - avatar
9 Antworten
+ 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/
30th Dec 2019, 8:20 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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.
30th Dec 2019, 5:28 PM
HonFu
HonFu - avatar
+ 2
We have the 'Code Playground' here. You can put the part with your loop there, save it and copypaste the link here.
30th Dec 2019, 5:36 PM
HonFu
HonFu - avatar
0
How can I show you the code, it's impossible to add image in this comment section.
30th Dec 2019, 5:33 PM
Anmol Kumar
Anmol Kumar - avatar
0
If you can give your email id , i can mail you the image.
30th Dec 2019, 5:34 PM
Anmol Kumar
Anmol Kumar - avatar
30th Dec 2019, 5:44 PM
Anmol Kumar
Anmol Kumar - avatar
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;
30th Dec 2019, 6:09 PM
Jayakrishna 🇮🇳
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); } } }
30th Dec 2019, 9:30 PM
ISOSIYA WARMATE
ISOSIYA WARMATE - avatar
- 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
31st Dec 2019, 1:23 PM
Abel
Abel - avatar