Submitted by prashant_agr on Sun, 2007-05-06 14:50.
The break statement breaks out of the smallest enclosing for or while loop.
The continue statement continues with the next iteration of the loop.
>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
Re: How does brake and continue work in Python ?
The break statement breaks out of the smallest enclosing for or while loop.
The continue statement continues with the next iteration of the loop.
>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...