While Statement Looping Examples
def while_example_1():
"""Demonstrates that a while statement completes a full loop before evaluating
its test statement.
"""
def _test(x):
y = False
if x == 2:
y = True
print(x, y)
return y
t = False
while t == False:
t = _test(0)
t = _test(1)
t = _test(2)
t = _test(3)
t = _test(4)
t = _test(5)
t = _test(6)
t = _test(7)
break
def while_example_2():
"""A contrasting example to while_example_1 to demonstrate that a while statement
completes a full loop before evaluating its test statement.
"""
t = False
loop = x = 0
print('while loop set to terminate at 2\n')
while t == False:
loop += 1
print('start of loop %s: x == %s, t == %s' % (loop, x, t))
x += 1
if x == 2:
t = True
print('end of loop %s: x == %s, t == %s' % (loop, x, t))