Apr-12-2020, 06:22 PM
Found these examples in the documentation.Was looking up For/Else loops. They both use a break statement. Was a bit confused why the break statement was there so ran them both without the break statements and the output is the same! So why have them?
Example 1.
Example 1.
for n in range(2, 10): for x in range(2, n): if n % x == 0: print(n, 'equals', x, '*', n/x) breakExample 2 - which is adding an else statement to the above (which I get)
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')