• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about variables in the while construct  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New to java/programming and i cant understand why the pen variable does not display the the correct value ... For example for input 1 ; 2 ; 3 ; 4 both variables will display 10 and i dont understand why pen does not have the value 6 .Would appreciate someone enlighting this for me .

 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what happens when readInt returns 0?
r = 0;
pen = sum; // pen = 10
sum = sum + r; // sum = 10 + 0


THEN, while condition is checked and the loop exits.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing it out , i imagined incorrectly that the Read was outside of the while loop and there would not be a 0 loop
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!