Differentiate between break and continue statements. - Students Free Notes

Differentiate between break and continue statements.

The break and continue statements are used to control the flow of execution in loops, but they serve different purposes:

  • break: This statement is used to exit a loop completely, terminating it regardless of whether the loop’s condition has been satisfied. It is often used when a certain condition is met, and there’s no need to continue executing the remaining iterations of the loop. Example:

for(int i = 1; i <= 10; i++) {
if(i == 5) {
break; // Exits the loop when i is 5
}
printf(“%d “, i);
}
// Output: 1 2 3 4

  • continue: This statement is used to skip the current iteration of the loop and move to the next iteration. It does not terminate the loop but simply skips the remainder of the code inside the loop for the current iteration. Example:

for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue; // Skips the current iteration when i is 3
}
printf(“%d “, i);
}
// Output: 1 2 4 5