Home > Article > Backend Development > The difference between break and continue when jumping out of a loop
The difference between break and continue when jumping out of a loop
● break: Break out of this loop and execute the statements below this loop.
● continue: Terminate this cycle and enter the next cycle.
break example:
#include<stdio.h> int main() { int num = 0; int i = 0; for (int i = 0; i < 10; i++) { if (num == 5) { break; num += 2; } num += 1; } printf("%d\n", num); system("pause"); return 0; }
The output result is 5.
When num =5, the program jumps out of the loop directly and executes the output statement, so the output is 5.
continueExample:
#include <stdio.h> int main() { int num = 0; int i = 0; for (int i = 0; i < 10; i++) { if (num == 5) { num += 2; continue; } num += 1; } printf("%d\n", num); system("pause"); return 0; }
The output result is 11.
Recommended learning: c language video tutorial
The above is the detailed content of The difference between break and continue when jumping out of a loop. For more information, please follow other related articles on the PHP Chinese website!