Continue Statements and While Loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Continue Statements and While Loops

When I use "Continue Statements" in while Loops, I get this error : Terminated Execution Timed Out! public class Loop_Control_Statements { public static void main (String[]args) { int z = 45; while(z<=50) { if(z==49) { continue; } System.out.println(z); z++; } } }

11th Jul 2020, 9:52 AM
H-J
H-J - avatar
2 Answers
+ 5
i got ur bug ur mistake is that u increment the value of z after the "continue" statement look at ur code when z == 49, ur condition got true and the code lines after got skipped and loop restarts with the same value of z that is 49 and this created a infinity loop that give this timeout error becouse it was taking too much time for complier to process a infinity loop and it ends ur codes try to put the z++ before the condition and all things works fine
11th Jul 2020, 9:58 AM
Abhay
Abhay - avatar
+ 3
The value increment for variable <z> is never reached. The condition z == 49 is the cause, value of <z> is not incremented because once <z> equals to 49 execution flow will always enter the 'z == 49' branch, and value of <z> is never incremented. The loop is stuck in infinity, and SoloLearn Code Playground safety mechanism terminates your code because the loop will never end : )
11th Jul 2020, 10:03 AM
Ipang