Module 3: Control Flow - Conditional Execution and Loops
Controlling Loop Execution: `break`, `continue`, and `else` in Loops
-
break
: Immediately exits the current loop (bothfor
andwhile
). -
continue
: Skips the rest of the current iteration and proceeds to the next iteration of the loop. -
else
clause in loops: An optionalelse
block can be used with loops. For afor
loop, theelse
block executes after the loop finishes all iterations, but only if the loop was not terminated by abreak
statement. For awhile
loop, theelse
block executes when the loop's condition becomesFalse
, but not if the loop was exited withbreak
. Example withfor
:for i in range(3): print(i) else: print("Loop finished normally")