+ 2
for(int x=10; x<=40; x=x+10) { if(x == 30) { continue; } System.out.println(x); }
Help me to explain this program I can't understand continue statement
3 Antworten
+ 1
* `continue` takes you back to the beginning of the loop. The first statement inside the loop body will be executed next.
for(int x = 10; x <= 40; x = x + 10)
{
if(x == 30) ←←
{ ↑
continue; →→
}
System.out.println(x);
}
* `break` takes you out of the loop even when the loop condition still satisfies. The first statement following the for loop body will be executed next.
for(int x = 10; x <= 40; x = x + 10)
{
if(x == 30)
{
break; →→→→→→→→→→
} ↓
System.out.println(x); ↓
} ↓
System.out.println("Done!");←←
0
continue just means do not do anything and go back to the start to execute the next statement.
So until x=30, 10 20 is printed and when x=30 it again goes to for loop and now x becomes 40 which is printed.
So your output would be 10 20 40
0
Break means come out of the loop and continue means stay in the loop.