Flow Control
Flow control in Python allows you to modify the behavior of loops and conditionals using special statements like pass
, continue
, and break
. These statements help manage how and when certain parts of your code are executed.
The pass
statement acts as a placeholder. It does nothing but ensures the code is syntactically correct.
for letter in 'python': if letter == 'h': pass # Does nothing print(letter)
continue
Section titled “continue”The continue
statement skips the rest of the code inside the loop for the current iteration and moves to the next iteration.
for number in range(5): if number == 2: continue # Skips number 2 print(number)
The break
statement immediately ends the loop, regardless of the loop condition.
for number in range(5): if number == 3: break # Exits the loop when number is 3 print(number)