Loop Control in Python

For ciklusListák

Loops repeat code, but sometimes you need more control: exit early, skip an iteration, or run code only when the loop finishes normally.

Python provides break, continue, pass, and an else clause for loops. These statements help write more precise and efficient code.

We'll look at each one with examples in both while and for loops.

break – Exit the Loop Early

break immediately stops the loop, even if the condition is still true. Execution continues after the loop.

Common use: stop when a condition is met (like finding an item).

In for loop:

continue – Skip to Next Iteration

continue skips the rest of the current iteration and jumps to the next one.

Common use: skip certain values (like even numbers).

In for loop:

pass – Do Nothing

pass is a null operation – it does nothing. Use it as a placeholder when code is required syntactically.

else Clause in Loops

The else block runs when the loop finishes normally (condition false or sequence ended) – but not if break was used.

Useful for 'search not found' cases.

Quick Quiz

Az anyagokat átnéztük és ellenőriztük, de hibák továbbra is előfordulhatnak. A tartalom kizárólag oktatási célt szolgál, ezért saját felelősségre használd, és szükség esetén ellenőrizd más forrásokkal is.

✨ Kérdezd Larát — a tanulási partnered

Fedezd fel a személyre szabott tanulási támogatást. Lara elmagyarázza az anyagot, összefoglalja a témákat és megválaszolja a kérdéseidet — az Go csomagtól elérhető.


Lara segít gyorsabban tanulni — kizárólag a ReadyTools Go, Plus és Max tagoknak.

Kövesd nyomon a fejlődésed 🚀

Tanulj egyszerűbben utad nyomonkövetésével teljesen ingyen.