鶹Լ

When iterations don’t run

Sometimes when using a condition-controlled (WHILE) loop there may be a situation where the code contained within the iteration is never run. This is because the condition is tested at the start of the iteration.

If the condition is never true, then the code within the iteration will never be run.

Consider this Python (3.x) program:

total = 0
answer = input("Any numbers to add? yes/no ")
while answer == "yes":
	number = int(input("Type in a number: "))
	total = total + number
	answer = input("Any more numbers? yes/no ")
print("The total is: ")
print(total)

If, when the program is run, we entered ‘no’ to the question “Any numbers to add?” then the condition in line 3 would never be met. The program would skip all the code within the iteration and move onto the last line.