How many times the loop will execute in the following program int p=200; while(true) { if(p<100) break; p=p-20; }
Answers
Answered by
24
Yes Bro Here Is Your Answer------The Loop Will Be Executed For 6 Times.....As The Final Value of p will Be 80 which Is Less than 100.....then It counter the Break Statement And Left the loop....
Answered by
47
Hey There!!
Here, we are given:
int p=200;while(true){ if(p<100) break; p=p-20;}
This code starts with p=200.
At each iteration of the loop, the value of p is decreased by 20. The Loop breaks only when p<100.
Let's go step-by-step:
1st iteration -> p=180 [i.e. p=180 is the end of first iteration]
2nd iteration -> p=160
3rd iteration -> p=140
4th iteration -> p=120
5th iteration -> p=100
6th iteration -> p=80
Now, the value of p is 80.
But there is no condition to enter the while loop.
while(true) is set. So, no matter what value of p, it will enter the loop.
Now, p=80 enters the Seventh Iteration. Now, INSIDE the loop, we are checking if p<100. Now this if condition is checked, and it comes out to be true. The loop is finally broken so that it cannot enter the Eighth Iteration.
Finally, The Loop completes Seven Iterations.
So, your answer is 7.
Hope it helps
Purva
Brainly Community
Here, we are given:
int p=200;while(true){ if(p<100) break; p=p-20;}
This code starts with p=200.
At each iteration of the loop, the value of p is decreased by 20. The Loop breaks only when p<100.
Let's go step-by-step:
1st iteration -> p=180 [i.e. p=180 is the end of first iteration]
2nd iteration -> p=160
3rd iteration -> p=140
4th iteration -> p=120
5th iteration -> p=100
6th iteration -> p=80
Now, the value of p is 80.
But there is no condition to enter the while loop.
while(true) is set. So, no matter what value of p, it will enter the loop.
Now, p=80 enters the Seventh Iteration. Now, INSIDE the loop, we are checking if p<100. Now this if condition is checked, and it comes out to be true. The loop is finally broken so that it cannot enter the Eighth Iteration.
Finally, The Loop completes Seven Iterations.
So, your answer is 7.
Hope it helps
Purva
Brainly Community
QGP:
Please mark as brainliest if you like it
Similar questions