Computer Science, asked by saireddy49472, 9 months ago

9. How many times will the loop run?
i=2 while(i>O): i=i-1​

Answers

Answered by duhanmukes
4

Answer:

loop will run

ten times

Answered by varshamittal029
0

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 i > 0, the loop will get executed:

First iteration:

i=i-1

i=2-1\\i=1

Again the condition will be checked.

while(i > 0)

Since, i=1 > 0, so again loop will run.

Second iteration:

i=i-1\\i=1-1\\i=0

Now i=0, therefore the condition while(i > 0) becomes False for the next iteration.

Hence the loop will run 2 times when i=2 and i=1.

Similar questions