학습 자료

Loop Controls (break, continue, pass)

Loops let us repeat code — but sometimes we need more control. What if you want to exit a loop early, skip part of it, or leave space for future logic?

Python provides three helpful keywords: break, continue, and pass.

Let’s go through each of them with examples.


1. break: Stop the Loop Immediately

Use break when you want to exit the loop entirely, no matter how many items are left.

for number in range(1, 10): if number == 5: break print("Number:", number)

Explanation:

  • This loop prints numbers from 1 to 9.
  • But when number == 5, it hits break and exits the loop.
  • So the output is: 1, 2, 3, 4

2. continue: Skip the Current Step

Use continue when you want to skip one iteration and move on.

for number in range(1, 6): if number == 3: continue print("Number:", number)

Explanation:

  • This skips number == 3, but keeps looping.
  • Output is: 1, 2, 4, 5

3. pass: Placeholder That Does Nothing

pass is used when you need a block of code syntactically, but don’t want to write anything yet.

for letter in "data": if letter == "t": pass print("Letter:", letter)

Explanation:

  • When letter == "t", the program does nothing — just moves on.
  • This is useful as a placeholder for future logic.

Summary

KeywordWhat It Does
breakExits the loop completely
continueSkips to the next loop iteration
passDoes nothing (used as placeholder)

What’s Next?

Up next, you’ll learn how to define and use functions — an essential skill for writing clean, reusable Python code.

Quiz
0 / 1

Which Python keyword would you use to skip the current iteration of a loop?

break

pass

continue

exit

학습 자료

AI 튜터

디자인

업로드

수업 노트

즐겨찾기

도움말

코드 에디터

코드 실행
코드 생성

실행 결과