analyse the given program and write the number of times the loop will execute . give output as well
please answer please
Answers
The loop will be executed 0 times and the output will be nothing...because the condition p >= 150; is always false.
Solution :
for(int p = 200; p > = 150; p = p - 10)
{
if(p % 20 = = 0)
continue;
System.out.println(p);
}
The loop will run 6 times.
Output :
190
170
150
Explanation :
First the value of P is 200.
So,
We have to check that if (200 % 20 = = 0).
Yes it is true because after dividing 200 by 20 we are getting the remainder 0.
The snippet is having continue.
So, the values which satisfy the condition (p % 20 = = 0) will not be printed because the loop will once the satisfies.
Let's Try Running It :
When p = 200
200 > 150
(200 % 20 = = 0)
True so it will continue.
When p = 190 (decrements by 10)
190 > 150
(190 % 20 = = 0)
False so the value will be printed.
When p = 180
180 > 150
(180 % 20 = = 0)
True so it will continue.
When p = 170
170 > 150
(170 % 20 = = 0)
False so the value will be printed.
When p = 160
160 > 150
(160 % 20 = = 0)
True so it will continue.
When p = 150
150 = 150
(150 % 20 = = 0)
False so the value will be printed.
when p = 140
140 ≠ 150 & 140 < 150
It does not satisfy the condition so the loop will end.
Hence,
Output :
190
170
150
The loop will run 6 times.