- 1
Do While Output Based Question(Query)
Here's a code fragment: int x=3, y= 10; do{ JOptionPane.showMessageDialog(null, x+y); x= x+1; y= y-2; } while (x<=y); System.out.println("x=" + x); The question is how many times the loop will execute and what will be the output? I know that the loop will execute 3 times (13,12,11) and x= 16. My query is how is x=16? Can anyone briefly explain to me how x will be 16? Thanks alot in advance.
3 Answers
+ 5
@Gaurav is right.
Super checked!!
+ 20
1st loop) x=3 , y=10 [true]
2nd loop)x=4 , y=8 [true]
3rd loop) x=5, y=6 [now x+1 will not be <= y-2 ... ie 6 is not <=4]
//loop stops
so x will be 6 , y will be 4 & loop runs 3 times
/* x will be 6 , not 16 ... might u read 16 as 1 from 11 & 6 from x */
//hope it helps âș
- 1
Thanks heaps for clarifying. I really appreciate it