Loading...
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?
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.
Sign in as a tutor to answer this doubt.