Difference between break and continue statement.
- break - loop is terminated
- continue - stops the normal flow of loop and returns to the loop without executing statement after the continue statement.
public class MyBreak
{
public static void main(String[] args)
{
for(int i=0; i<30; i++)
{
System.out.print(i);
if(i == 10)
{
System.out.println(" \nBreaking the loop when i == 10 ");
break;
}
System.out.println(" NEXT...");
}
System.out.println("End of first loop\n");
for(int i=0; i<30; i++)
{
System.out.print(i);
if(i == 10)
{
System.out.println(" \nContinue statement when i == 10");
continue;
}
System.out.println(" and still iterating...");
}
}
}
As you can see break statement terminated loop when i == 10.
Continue statement only terminated flow when i == 10 and returned to the iteration.
No comments:
Post a Comment