+ 3

Java question . Why the answer is coming 80 instead of coming 200. Please explain me

class xyz{ public static void main (){ int p =200; while (true) { if(p<100) break; p=p-20; } System.out.print(p); } }

25th Nov 2020, 11:34 AM
Abhishek Topno
Abhishek Topno - avatar
6 odpowiedzi
+ 4
Dry run p=200 200<100 //false 200-20=180 180<100// false . 160<100//false . 140<100//false . 120<100//false . 100<100//false 100-20=80 80<100// true and P=80 Break statement run and while loop terminat
25th Nov 2020, 12:09 PM
Prathvi
Prathvi - avatar
+ 6
The condition inside while loop get break when p<100 ,otherwise ithe statement p=p-20 will run, in simple words program will subtract 20 from variable p and stop when less than 100.
25th Nov 2020, 11:42 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 2
Abhishek Topno Of course I could it explain, but if you could it test yourself you could learn much more as I already said. Debugging is the routine process of locating and removing bugs, errors or abnormalities from programs. It's a must have skill for any developer. With such as tool you can also see how your code works as well as states of your variables. If you do not have a environment with a debugger, you could test it here on the SL Playground with a few additional print statements. I.e. you can set one print statement for p variable within the while loop at its end. So you could see what happends with the p etc.
25th Nov 2020, 4:00 PM
JaScript
JaScript - avatar
+ 2
Thx
25th Nov 2020, 5:14 PM
Abhishek Topno
Abhishek Topno - avatar
+ 1
You can test it yourself then can you see how that works. Try to do it. So can you much learn.
25th Nov 2020, 11:48 AM
JaScript
JaScript - avatar
+ 1
Can you explain me
25th Nov 2020, 12:00 PM
Abhishek Topno
Abhishek Topno - avatar