I did not like the answers posted so far, since they all require the body of the cycle to be changed, which can be annoying / risky if the body is really complex, so here is a way to do this using the flag. replace _break with found or something else meaningful for your use case
_break = True for n in range(15): if n == 100: break else: _break = False if _break: print(n)
Another possibility, if it is a function that does nothing, if the loop does not find a match, is in return in the else: block else:
for n in range(15): if n == 100: break else: return print(n)
John la rooy
source share