Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
break, continue, and passLoops are designed to execute iteratively until their condition turns False or their sequence runs out of items. However, real-world algorithms frequently encounter conditions where the standard flow must be redirected:
break).continue).pass).Python provides three distinct keywords—break, continue, and pass—to manage these granular control flow requirements.
+-------------------------------------------------------------------------------+
| LOOP CONTROL REAL-WORLD ANALOGIES |
+-------------------------------------------------------------------------------+
1. BREAK (EARLY TERMINATION):
- An Indian Railways TTE walks down the 3AC coach searching for Dr. Verma
who was paged for a medical emergency.
- TTE inspects Berth 1, Berth 2, Berth 3...
- At Berth 14, TTE finds Dr. Verma:
-> BREAK! Stop checking Berths 15 through 72. Mission accomplished!
2. CONTINUE (SKIP ITERATION):
- The TTE is checking ticket concessions for passenger discounts.
- Passenger at Berth 15 has an infant (below 5 years):
-> Infants don't require ticket punching.
-> CONTINUE! Skip fee calculations for this passenger, jump straight
to Berth 16.
3. PASS (NULL OPERATION / PLACEHOLDER):
- Berth 17 is marked "VIP QUOTA - TO BE BOARDED AT KANPUR".
- Currently empty. TTE does nothing right now, but cannot skip the berth
from the manifest records.
- PASS! Maintain the placeholder without performing any action.
+-------------------------------------------------------------------------------+break Statement (Immediate Loop Termination)The break statement halts the execution of the innermost loop immediately and moves program control to the statement directly following the loop block:
Output:
Notice how Python halted at index 2 without wasting time scanning the remaining elements.
continue Statement (Skip to Next Iteration)The continue statement rejects all remaining code in the current iteration and jumps directly back to the top of the loop to begin the next iteration:
Output:
pass Statement (The Syntactic Null Operation)In Python, code blocks defined by indentation cannot be empty. Writing an empty if, for, while, or def block causes an IndentationError: expected an indented block.
The pass statement is a null operation (no-op): when executed, absolutely nothing happens. It serves as a syntactical placeholder while scaffolding architecture:
Output:
| Attribute | break | continue | pass |
|---|---|---|---|
| Action | Terminates the entire loop immediately. | Skips the rest of current cycle; jumps to next iteration. | Does nothing; executes following line normally. |
| Execution Continues At | Statement outside the loop. | Header of the next loop iteration. | Next statement in the same block. |
| Primary Use Case | Early search termination, circuit-breakers. | Data validation, discarding noise/errors. | Code scaffolding, empty stubs, placeholder functions. |
| Leaves Loop? | YES | NO | NO |
| Best Practice (DO) | Dangerous Anti-Pattern (DON'T) |
|---|---|
DO use break when searching for an element to prevent wasteful $O(N)$ CPU cycles. | DON'T use break excessively to replace clean loop conditions; keep loop invariants explicit. |
DO use continue at the top of loops to filter out bad data early (guard clause style). | DON'T stack 5 nested if statements when a single continue at the top cleanly skips invalid entries. |
DO use pass when creating stub methods or satisfying abstract syntax requirements. | DON'T confuse pass with continue; pass does NOT skip to the next iteration! |
break terminates the innermost enclosing loop prematurely and resumes execution immediately outside the loop.continue abandons the remainder of the current iteration and jumps directly to the beginning of the next cycle.pass is a syntactical placeholder (no-op) required because Python does not allow empty code blocks.break occurs inside an inner nested loop, only that specific inner loop terminates.pass does not alter control flow; code continues running straight through to the next line.If you forget to update your loop counter inside a while loop, the condition remains True forever:
Removing or inserting items in a list while looping over it causes skipped elements:
A. 1 2 3 B. 1 2 C. 1 2 4 5 D. 3 4 5
A. 1 2 B. 1 2 4 5 C. 1 2 3 4 5 D. 3
pass keyword in Python?A. It terminates the entire script with exit code 0 B. It skips the current iteration of the loop like continue C. It acts as a null operation (no-op), serving as a syntactic placeholder without altering execution flow D. It passes variables from child to parent scope
pass statement?A. The loop resets back to iteration 0 B. Python skips all code beneath pass in that block C. The statement following pass on the next line executes normally D. An IndentationWarning is emitted
break. What happens to the outer loop?A. Both loops terminate completely B. Only the inner loop terminates; the outer loop continues running normally C. The outer loop resets to its initial value D. Python raises a LoopInterrupt exception
Build an automated tax audit and data sanitation pipeline for the Indian Goods and Services Tax (GST) Network.
The audit pipeline processes a batch of incoming vendor invoices. The system must apply fine-grained loop control:
amount_inr < 0) or an invalid GSTIN string (len(gstin) != 15), print a warning and use continue to skip the record."SUSPECTED_HAWALA_SHELL" in the remarks, the system must immediately trigger an emergency compliance halt using break to freeze the audit batch for regulatory investigation."SEZ_TAX_EXEMPT", use pass as a placeholder for the future SEZ duty refund calculator, but still allow normal processing.In the next lesson, we will continue your Python learning journey with Loops with Else (13: Loops).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Nested Loops | Loops with Else |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.