+ 10

{Help} Explain This Please😉

int x=0; int y=2; do{ x+=y; } while (x<4); System.out.println(++x); Output is 5 Explain it please😉😉

6th Jan 2018, 5:16 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
16 Antworten
+ 25
@Khairul loop1)x=2 & condition is true loop2)x=4 & condition is false ... so loop stops print (++4) ... print 5 @Nitish x+=2; means x=x+2;
6th Jan 2018, 5:38 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 12
What about x+=y. ??
6th Jan 2018, 5:24 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 8
when x comes out of do-while loop, its value will be 4. In system.out.print method it is first incremented to 5 (since its prefix)and the printed to 5
6th Jan 2018, 5:23 AM
Qqqq
Qqqq - avatar
+ 8
first x=0. since its do-while, loop get executed once before condition, then x=2. then it check for condition, it is sactisfied. then again statement in loop is executed and x=4 now. x+=2 is equivalent to x=x+2
6th Jan 2018, 5:27 AM
Qqqq
Qqqq - avatar
+ 7
Thanks😉😉
6th Jan 2018, 8:58 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 7
because ++x will print value after incrementing x. hence, ++4=5.
6th Jan 2018, 11:38 AM
Dhruv garg
Dhruv garg - avatar
+ 6
The output of this code is 5 because the code will keep on running as long as x<4, but as soon. as x is equalled to 5 the program stop running.
6th Jan 2018, 6:40 AM
Joey
Joey - avatar
+ 5
x+=y means x=x+y
6th Jan 2018, 5:27 AM
VaiBhav
VaiBhav - avatar
+ 4
x =+y means x= x+y
6th Jan 2018, 5:25 AM
Nitish
Nitish - avatar
+ 4
on the first time we get x=0+2 // now the value of x sets to 2 entering while loop it gets incremented and then displayed as 3 ON 2 time now the value of x is 2 so now x=2+2 // this produces result x=4 on entering the while loop x value gets incremented to 5 and displayed
6th Jan 2018, 4:26 PM
Bharath Kalyan K B
Bharath Kalyan K B - avatar
+ 3
If you try to do it by step you have : x=0; y=2; x += y; (which means x = x +y;) which makes the x == 2 Then you check if x<4 (it is true, as 2<4), so you repeat what's inside the awhile brackets. So we again have x+= y, which as a result, gives x==4. Now the while condition is false, as 4 < 4 is a false statement, so the loop stops. Now we print the x value is 4 and we print ++x, which raises x's value by one and returns the new value, so it returns 5
6th Jan 2018, 9:01 AM
Giorgos Perogamvros
Giorgos Perogamvros - avatar
+ 2
@gaurav answer my question
6th Jan 2018, 5:42 AM
VaiBhav
VaiBhav - avatar
+ 1
Shohsanam First iteration of the loop: x=1+2=3 As 3< 7, the loop run for the second time. 2nd iteration: x=3+2=5 As 5<7, the condition is true. The loop iterates for 3rd time. 3nd iteration: x=5+2=7
28th Apr 2021, 1:31 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
0
correct answer for below question int x = 1; while (x <= 7 ) { System.out.println("in a loop"); x ++; }
12th May 2020, 9:13 AM
Avinash Gavane
0
Currect 9
16th Apr 2023, 9:36 AM
𝓡𝓪𝓰𝓱𝓪𝓿 𝓡𝓸𝓶𝓮𝓸
𝓡𝓪𝓰𝓱𝓪𝓿 𝓡𝓸𝓶𝓮𝓸 - avatar
- 1
How many times will this loop run? int x = 1; for ( ; x < 7; ) { x+=2; } WHAT IS THIS
28th Apr 2021, 8:57 AM
Tursunova Nasiba Alisher Qizi
Tursunova Nasiba Alisher Qizi - avatar