1. Loop Termination & Invariants
Preventing Infinite Loops
- A while loop requires three elements: initialization, condition check, and state progression towards termination.
- Failing to update state variables inside the loop body causes an infinite loop.
- Use `break` to exit the loop early when a target condition is met.
- Use `continue` to skip the remainder of the current iteration and re-evaluate the loop condition.
count = 0
while count < 3:
print("Iteration:", count)
count += 1
💡 Key Takeaway: Always verify that every loop branch guarantees progress toward condition failure or break.