Differences between ‘break’ and ‘continue’ statements
break continue
- break is a keyword used to terminate the loop or
exit from the block. The control jumps to next
statement after the loop or block.
- continue is a keyword used for skipping the current
iteration and go to next iteration of the loop
2.Syntax:
{
Statement 1;
Statement 2;
Statement n;
break;
}
2.Syntax:
{
Statement 1;
continue;
Statement 2;
}
-
break can be used with for, while, do- while, and switch statements. When break is used in nestedloops i.e. within the inner most loop then only the innermost loop is terminated.
-
This statement when occurs in a loop does not terminate it but skips the statements after this continue statement. The control goes to the next iteration. Continue can be used with for, while and do-while.
-
Example:
i = 1, j = 0;
while(i<=5)
{
i=i+1;
if(i== 2)
- Example:
i = 1, j = 0;
while(i<=5)
{
i=i+1;
if(i== 2)
break;
j=j+1;
}
continue;
j=j+1;
}