# Sometime you don't know when to exit end a loop in advance; in this case, use break statement to jump out of loop # Ex: take input from a use until they type "done" while True: line = input('> ') if line == 'done': break print(line) # the loop condition is True, which is always true, so it runs until it hits the break statement # if the use typed "done", the break statement exits the loop # Use 'break' in loops that iteratively compute numerical values: if abs(x - y) < epsilon: # epsilon is a very small number, e.g. 0.0000001 break
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter