← The C Language
Lesson 19 of 77 · 10 XP
break and continue
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
break leaves the nearest loop (or switch) immediately. continue goes on to the next step of the loop.
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0)
continue; // skip the even ones
if (i > 7)
break; // stop after 7
printf("%d ", i); // 1 3 5 7
}
break leaves only one level of nesting. To leave two loops at once, people use a flag or move the loops into a function with return. Sometimes goto is used for this. It's one of the few places where using it is considered acceptable.
Task. Read numbers until the end of the input. Skip negative ones, and stop at the first zero. Print the sum of the positive numbers met before the zero.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.