0

Java programming loop statements

int n =10, sum = 10; While(n--) { Sum += n--; } System. Out. Print on("sun =" + sum) ;

3rd Aug 2022, 10:50 AM
Deepthi Priya
Deepthi Priya - avatar
2 Réponses
+ 3
ok, it would be great to have more details. What is your problem? What are you trying to achieve. I'm noticing that your while statement is wrong, what you might need is while(n-- > 0) also, tags are a bad place for code, but is great to specify the language you use, keep that in mind for your next post!
3rd Aug 2022, 11:10 AM
Apollo-Roboto
Apollo-Roboto - avatar
0
Hi there, I am not quite sure what you want to achieve... I have no idea why your sum starts at 10, but so be it. int n = 10, sum = 10; while(n > 0){ sum += n--; } System.out.print("sun = " + sum); This makes your code workable and gives you the result of 65, if you want to decrement twice you can write it like this: int n = 10, sum = 10; while(n-- > 0){ sum += n--; } System.out.print("sun = " + sum); } the the result is 35. I hope this helped you somehow
3rd Aug 2022, 1:06 PM
Lukas Schwab
Lukas Schwab - avatar