Break & Continue Keywords

Break –

  • To break a loop or a switch statement, we use the break keyword.
  • When a break statement encounters, the loop terminates immediately, and as a result, the program control goes to the next statement following the loop.
  • When a break statement encounters inside a switch statement, it terminates a case, and the program control goes to the next statement following the switch statement.
  • We should use it when we are not sure about the number of iterations.
  • Syntax – break;

Continue –

  • To immediately jump to the next iteration of the loop, we use to continue keyword.
  • When continue statement encounters inside a for loop, the control immediately jumps to the update statement.
  • When continue statement encounters inside a while loop or do/while loop, control immediately jumps to the Boolean expression.
  • Therefore, it is useful when we want to continue the loop; however, we do not want the rest of the loop body’s statements to execute for that particular iteration.
  • Syntax – continue;