Programming Fundamentals · Interview question

What's the difference between break and continue?

A strong answer

break exits the innermost loop immediately, control jumps to the statement after the loop's closing brace. continue skips the rest of the current iteration and jumps to the next one: in a for loop it runs the update clause first, then tests the condition; in a while loop it goes straight to the condition test. Use break when you've found what you came for and don't want to keep searching. Use continue to skip the body for iterations that don't pass a guard, which keeps the meaningful code at one indentation level.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

for Loops, break & continue

The counted loop you'll write most often, plus the two escape hatches: break to leave early, continue to skip an iteration.

More for Loops, break & continue questions

Browse all 472 interview questions