Quick actions

cmd+k|ctrl+k

Navigation

Languages

Lecture 7 While loop

Snippet info

Language

Python

Visibility

public

Author

tonyngswell

Created

2026-04-30T12:20:55.337656Z

Updated

2026-04-30T14:00:28.788373Z

x=5
while x > 0:
    print(f"Top of loop, x is: {x}")
    
    # Check if x is 4
    if x == 4:
        x = x - 1
        print("Continuing... skipping the rest of this block!")
        continue  # Jumps back to 'while x > 0'
    
    # Check if x is 3
    if x == 3:
        print("Breaking... exiting the loop entirely!")
        break     # Jumps out of the loop to the end of the script
        
    # This only runs if 'continue' or 'break' weren't triggered
    x = x - 1 #this line is the Engine, making the loop a decrement
else:
    # This will NOT print because the loop was 'broken'
    print("out of while")
    

INFO