Table of contents

 3. Miscellaneous Flow Control Statements.

The break statement is used to terminate the execution of a loop when a specific condition is met.

  • The break statement is always used within loop statements.
  • When a break statement is executed inside a loop, the Python Virtual Machine (PVM) exits the loop. It continues executing other statements in the program, skipping the corresponding else block of the loop.
# syntax for varname in iterable_object: # Loop body statements if test_condition: break else: # Else block statements (executed only if the loop completes normally, i.e., no break) # Other statements in the program
# using a for loop lst = [100, 220, 330, 110, 500, 300, 2200] for i in lst: if i > 450: break print(i) # Print each element until the break is encountered else: print("Loop completed without break")
while test_condition: # Loop body statements if break_condition: break else: # Else block statements (executed only if the loop completes without a break) # Other statements in the program
# using a while loop lst = [100, 220, 330, 110, 500, 300, 2200] i = 0 while i < len(lst): if lst[i] > 450: break print(lst[i]) i += 1 else: print("Loop completed without break")
  • continue is a Python keyword.
  • The purpose of the continue statement is to skip the remaining statements in the current iteration of a loop and move control to the start of the next iteration.
for varname in iterable_object: # Loop body statements if test_condition: continue # Statements after continue (skipped when continue is executed) # Other statements in the program
lst = [100, 220, 330, 110, 500, 300, 2200] for i in lst: if i > 450: continue # Skip elements > 450, move to next iteration print(i) # Print elements <= 450 # Other statements in the program print("Program continues here")
while test_condition1: # Loop body statements if test_condition2: continue # Statements after continue (skipped when continue is executed) # Other statements in the program
lst = [100, 220, 330, 110, 500, 300, 2200] i = 0 while i < len(lst): if lst[i] > 450: i += 1 continue # Skip elements > 450, move to next iteration print(lst[i]) # Print elements <= 450 i += 1 # Other statements in the program print("Program continues here")

The pass statement serves as a no-operation placeholder in Python loops (e.g., for or while), allowing you to write syntactically valid code when no action is needed for a condition, such as in an if block within a loop, without affecting the loop’s execution.

Unlike break (which exits the loop) or continue (which skips to the next iteration), pass does nothing and allows all subsequent statements in the loop body to execute normally, maintaining the loop’s regular flow.

for varname in iterable_object: # Loop body statements if test_condition: pass # No action taken, continues with the rest of the loop # Other statements in the loop # Other statements in the program
lst = [100, 220, 330, 110, 500, 300, 2200] for i in lst: if i > 450: pass # Placeholder for future code, no action taken else: print(i) # Print elements <= 450 # Other statements in the program print("Program continues here")
while test_condition1: # Loop body statements if test_condition2: pass # No action taken, continues with the rest of the loop # Other statements in the loop # Other statements in the program
lst = [100, 220, 330, 110, 500, 300, 2200] i = 0 while i < len(lst): if lst[i] > 450: pass # Placeholder for future code, no action taken else: print(lst[i]) # Print elements <= 450 i += 1 # Other statements in the program print("Program continues here")