9. How many times will the loop run?
i=2 while(i>O): i=i-1
Answers
Answer:
loop will run
ten times
Concept:
In Python, the while loop iterates through a block of code as long as the test expression (condition) is true.
Given program:
i=2
while(i>0):
print("1")
i = i - 1
Find:
How many times will the loop run?
Solution:
The test expression is checked first in the while loop. Only if the test expression evaluates to True is the body of the loop entered.
The test expression is evaluated again after one iteration. This procedure is repeated until the test expression returns False.
In the given program, the initial value of i is 2.
As , the loop will get executed:
First iteration:
Again the condition will be checked.
Since, , so again loop will run.
Second iteration:
Now , therefore the condition becomes False for the next iteration.
Hence the loop will run 2 times when and .