Theory
Found it. Why keep looking?
The marks program searches 60 records for roll number 42. The loop finds it at position 7... and then dutifully checks records 8 through 60, learning nothing.
Another day: computing the class average, but absent students are marked -1, and those must not poison the total.
Two different needs: abandon the loop entirely, and skip just this round. C ships both, plus one wild ancestor best kept in its cage.
Theory
Leaving the queue vs skipping a counter
You are walking a line of counters. break is walking OUT of the hall: whatever counters remain, you are done with all of them. continue is skipping ONE counter ("this one is closed, move on") while staying in the line for the rest. And goto is teleporting anywhere in the building by shouting a label, thrilling, and precisely how buildings descend into chaos.
Theory
The three, formally
- break; ends the enclosing loop (or switch) immediately; execution resumes after its closing brace. In nested loops it exits one level only.
- continue; abandons the REST of the current iteration and jumps to the next round. In a
forloop, the update (i++) still runs, then the condition is tested. - goto label; jumps unconditionally to
label:anywhere in the function. Legal C, but it dissolves the sequence/selection/iteration discipline into spaghetti; modern code allows it almost never (one classic pardon: escaping deep nesting).
Practical
break finds, continue filters
#include <stdio.h>
int main() {
int marks[6] = {70, -1, 55, 90, -1, 62};
int i, total = 0, present = 0;
for (i = 0; i < 6; i++) {
if (marks[i] == -1)
continue; /* absent: skip this round */
total += marks[i];
present++;
}
printf("Average of %d students: %d\n", present, total / present);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Trace the filter
Walk the loop above. Which indexes reach total += , what are total and present at the end, and what prints?
Show the answer
Indexes 1 and 4 hit continue and contribute nothing. Indexes 0, 2, 3, 5 add 70 + 55 + 90 + 62: total = 277, present = 4, printing Average of 4 students: 69 (integer division).
Note what continue did NOT do: it never ended the loop; it only fast-forwarded past the absentees. That distinction is the whole exam question.
Quiz
Inside a loop processing 10 items, what is the difference between hitting break and hitting continue at item 4?
- break abandons items 4-10 entirely; continue skips item 4 and processes 5-10
- Both stop the loop, break just does it faster
- continue exits the loop; break skips one item
- break restarts the loop from item 1
Show the answer
break abandons items 4-10 entirely; continue skips item 4 and processes 5-10
break is OUT: the loop is over, items 5 to 10 never happen. continue is NEXT: only the remainder of round 4 is skipped, and the loop lives on. Option C swaps them, the standard trap. One-line memory: break = leave the hall, continue = skip one counter.
Quiz
A break executes inside the INNER loop of a nested pair. What stops?
- Only the inner loop; the outer continues its next iteration
- Both loops stop at once
- Only the outer loop stops
- The whole program terminates
Show the answer
Only the inner loop; the outer continues its next iteration
break exits exactly ONE level: the loop that immediately encloses it. The outer loop never notices and proceeds with its next round. Exiting both levels needs a flag variable (or, the one classically pardoned goto). "Does break exit both nested loops?" is a reliable exam trick, and the answer is no.
Watch out
Where marks leak
Swapping break and continue in trace questions, slow down and label each. Assuming break exits ALL nested levels: one level only. Writing that continue skips the for update: i++ still runs, which is why continue in a for loop rarely hangs, but continue in a while loop placed before the manual i++ CAN loop forever, a subtle favourite. And in exams, call goto "legal but discouraged: it creates spaghetti code".
Theory
Unit 2 closes
Decisions, loops, nesting, and now the emergency exits: the marks program controls its own flow completely. What it still lacks is a decent MEMORY: sixty marks currently need sixty variables. Unit 3 fixes that in one stroke with arrays, one name, sixty numbered boxes, and every loop you have learned becomes ten times more useful.
Summary
Key takeaways
- break: exit the enclosing loop or switch immediately, one nested level only.
- continue: skip the rest of THIS round; the loop (and for's update) carries on.
- goto label: unconditional jump; legal, but spaghetti, discouraged.
- Uses: break ends a successful search; continue filters out bad records.
- continue before a while loop's manual increment can hang, for loops are safe.
- Memory hook: leave the hall (break), skip one counter (continue).