Given the following Python program,
#Version 1
x = 15
y = 8
while x - y > 0:
x -= 2
y += 1
print x, y
if x % y == 0: break
else:
print x, y
The output comes as:
13 9
11 10
9 11
9 11
First three lines get printed within while loop and last line (9 11) gets printed again as part of the else clause. Now, another variant:
#version 2
x = 15
y = 8
while x - y > 0:
x -= 2
y += 1
print x, y
if x % y == 0: break
else:
print x, y
And, now the output is:
13 9
13 9
11 10
11 10
9 11
9 11
See, each x, y pair is printed twice, one by the print statement above if and one because of the else clause. Does this mean first version allow else: to go outside while loop? Isn't that strange? What could be the reason behind?