while loop is repeating the step under it until you cannot fulfill the condition
Python code
inputNo = 10
i = 1
while (i <= inputNo): outputFile.write(str(i) + '\n') i = i + 1
Counting questionExampleinputNo = 10 : Assign/set 10 to inputNo (inputNo is the name that store value 10)i = 1 : Assign / set 1 to i (i is the name that store value 1)while (i <= inputNo):means that while you can fulfill the condition in the brackets , that's mean you can find i that is smaller or equal to the inputNonow i = 1, which is smaller or equal to 10so do the actions under itwrite i to outputFile ('\n' means enter to the next line)then i = i + 1 : means we add 1 to i, i is become 2 nowAgain while (i <= inputNo): now i = 2 which is smaller than 10then write 2 to output filei = 2 + 1 = 3Now i = 3, i is smaller than 10write 3 to output filei = 3 + 1 = 4Now i = 4, i is smaller than 10write 4 to output filei = 4 + 1 = 5...... (repeat the step)Now i = 8, i is smaller than 10write 8 to output filei = 8 + 1 = 9Now i = 9, i is smaller than 10write 9 to output filei = 9 + 1 = 10Now i = 10, i is equal to 10, fulfill the condition toowrite 10 to output filei = 10 + 1 = 11Now i = 11, i is not smaller than 10, i is not equal to 10we cannot fulfill the condition.So, break the loop. We will exit the loop which means we will not repeat the action under the loop.
No comments:
Post a Comment