intx = 2, y = 50;
do
{
++X
Y- = x++
}
while (x <= 10);
System.out.println(y);
How many times will the following loop executed??? Please explain
.
Answers
take one more variable count=0;
after Y-=X++ write
count=count++
after System.out.println(y);
print the count value, u vl get the no.of times the loop executes
Here is your Answer:
This loop will execute 5 times
Explanation:
++x means prefix where the number is incremented by 1.
x++ means postfix where the value of x is printed and then incremented by 1 in memory.
in do-while you can see both prefix and post fix operation are present so when the loop executes first time the value of x is incremented to 3 and 4 by two operator, 2nd time the value x is incremented to 5 and 6, 3rd time 7 and 8, 4th time 9 and 10 since the while loop condition is (x <= 10) where 10 is equal to 10 so it will execute one more time that is 5th time the value of x will be 11 and 12.
this clearly explains your question that the loop will be executed 5 times.