Back to Study Material Python

Control Flow

Control flow refers to the order in which individual statements, instructions, or function calls are executed within a program. By default Python executes ...

Control Flow

What is Control Flow?

Control flow refers to the order in which individual statements, instructions, or function calls are executed within a program. By default Python executes statements sequentially from top to bottom. Control flow structures allow you to alter this sequence: conditionals choose between paths, loops repeat a path, function calls jump to a defined block and return, and exceptions reroute execution to error-handling code. Mastering control flow is essential to writing non-trivial programs.

break Statement

The break statement immediately exits the innermost enclosing loop, regardless of whether the loop condition is still true. It is commonly used when searching for an item: once found, there is no need to continue iterating. Example: for item in collection: if item == target: print("Found!"); break. Without break, the loop would process all remaining items unnecessarily. break also works inside while loops and terminates them immediately.

continue Statement

The continue statement skips the rest of the current loop iteration and jumps back to the loop header to begin the next iteration. It is useful for filtering: for n in range(10): if n % 2 == 0: continue; print(n). This prints only the odd numbers because even numbers trigger continue and skip the print statement. continue is often used to avoid deep nesting — instead of wrapping the rest of the loop body in an if block, you continue early.

The else Clause on Loops

Python uniquely allows an else clause on for and while loops. The else block runs only if the loop completed without hitting a break statement. This is useful for search patterns: for item in collection: if condition: break; else: print("Not found"). The else signals that the search exhausted all items without finding a match. If break was triggered, the else is skipped. This behaviour is confusing to many programmers coming from other languages.

Exception-Based Flow Control

Python uses exceptions for flow control more than most languages. When an error occurs, Python raises an exception that unwinds the call stack until a matching except clause is found. If no handler is found, the program prints a traceback and exits. The try-except-finally structure provides structured control over exceptional paths. Python's philosophy is "ask forgiveness not permission" (EAFP): try the operation, catch exceptions if they occur, rather than checking preconditions first.

Exam Tip

The most tested control flow question is about the loop-else clause: what does the else on a for loop do? It runs when the loop finishes without a break. Another common question: what is the difference between break and return inside a loop that is inside a function? break exits only the loop; return exits the entire function. Also know that sys.exit() exits the entire Python interpreter, while break and continue only affect loops.