Back to community
Computer ScienceClass 11 (Maharashtra HSC) Resolved

What is the difference between break and continue in a Python loop?

In my textbook both break and continue are used inside loops and I keep mixing them up in exams. Can someone explain the exact difference with a small example?

Asked by Rohan Verma 9 45d ago
Need a Computer Science tutor? Browse verified Computer Science tutors near you.

1 answer

Accepted answer
4

Both control loop flow but do opposite things. break stops the loop completely and jumps out, so no more iterations happen. continue skips only the rest of the current iteration and jumps back to the top to test the condition again, so the loop keeps running. Example: 'for i in range(1, 6): if i == 3: break; print(i)' prints 1 2 then exits at 3. But 'for i in range(1, 6): if i == 3: continue; print(i)' prints 1 2 4 5, skipping only 3. Remember it as: break = leave the loop, continue = skip to the next round. In exams, state clearly that break ends the loop while continue ends only the current pass.

M
Meera Joshi
335 pts· 45d ago

Sign in as a tutor to answer this doubt.