Page 88 - iPro_trackGPT_V5_Class8
P. 88
{
break;
}
else
{
System.out.println("The value of i is: " + i);
}
}
System.out.println("Loop is terminated");
}
}
In the preceding program, the for loop terminates when the value of the variable i becomes
5. This is because the break statement gets executed when the condition given with the ‘if’
keyword becomes true (i == 5), which causes the loop to stop executing and come outside
the loop.
The continue Statement
The continue statement skips the rest of the current loop cycle and goes directly to the next
iteration, without running the code that comes after it in that cycle.
For example,
public class ContinueStatement
{
public static void main(String args[])
{
int i;
for(i=1; i<=10; i++)
{
if(i==5)
{
System.out.println("Loop is continued");
continue;
}
else
{
System.out.println("The value of i is: " + i);
}
}
}
}
86 TrackGPT iPRO (V5.0)-VIII

