Knowee
Questions
Features
Study Tools

How many times will the loop run?i=2while(i>0):    i=i-1

Question

How many times will the loop run?i=2while(i>0):    i=i-1

🧐 Not the exact question you are looking for?Go ask a question

Solution

The loop will run 2 times.

Here's the step by step explanation:

  1. Initially, i is set to 2.
  2. The condition in the while loop checks if i is greater than 0. Since 2 is greater than 0, the loop will run.
  3. Inside the loop, i is decremented by 1. So now, i becomes 1.
  4. The condition is checked again. Since 1 is still greater than 0, the loop will run again.
  5. Inside the loop, i is decremented by 1 again. So now, i becomes 0.
  6. The condition is checked again. This time, since 0 is not greater than 0, the loop will not run.

So, the loop runs a total of 2 times.

This problem has been solved

Similar Questions

What will be the output for the following code?i=0while(i<3):    j=0    while(j<3):        print(i,j,end=" ")        j+=1    print()0 00 10 20 0 0 0 1 2Infinite loopError

How many times following loop will be executed?void main(){   int i = 32766;   while (i<= 32767)   {        printf("%d\n",i);         i = i + 1;     }}a.2 timesb.1 timesc.infinite timesd.loop will not be executed

Will the following code ever end?i = 0while i > 0: print(i) i = i + 1

Please answer the following questions to assess your level: How many times is the following loop executed?1234 i=100 while(i<=200): print(i) i+=20

How many times will the following loop execute? What will be the final value?int a=1,x=0;do{x=a++ *a,}while(a<=5),System out.println(x);}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.