December 2000
Intermediate to advanced
816 pages
16h 57m
English
The break statement in Python terminates the current loop and resumes execution at the next statement, just like the traditional break found in C. The most common use for break is when some external condition is triggered (usually by testing with an if statement), requiring a hasty exit from a loop. The break statement can be used in both while and for loops.
count = num / 2 while count > 0: if (num % count == 0): print count, 'is the largest factor of', num break count = count - 1
The task of this piece of code is to find the largest divisor of a given number num. We iterate through all possible numbers that could possibly be factors of num, using the count variable and decrementing for every value that does NOT divide ...
Read now
Unlock full access