changing a local variable inside a while loop
So after I wrote out the first part of lesson 8, I got curious. Why didn't changing the variable s, inside the while loop, end the loop?
if the loop requires s to equal Y or y, and the new s in the loop is not Y or y, why didn't it shut the loop down?
I found very little to explain, but it seems what the great mighty google says is that a local variable changed inside the loop will not be saved... But the program will not loop again unless I add that last bit of code highlighted, code in which I can have the user answer yes again and have it restart.
Eclipse would not allow me to create a new String s = input(); into the loop, because it would change the local variable. But changing s to a new input ("yes") allows the program to run again. So clearly I changed the variable for the math, then changed it back again to yes....
shouldn't the loop stop the first time s is changed to not fit the statement?
Going to surf some more and see if anything pops up.
Solution: http://math.hws.edu/javanotes/c3/s3.html
The while loop doesn't evaluate if the statement is True/False until it has run to the end of the loop.
So you can change the variable as much as you want, but it won't stop the loop unless it evaluates to false at the end.
Solution: http://math.hws.edu/javanotes/c3/s3.html
The while loop doesn't evaluate if the statement is True/False until it has run to the end of the loop.
So you can change the variable as much as you want, but it won't stop the loop unless it evaluates to false at the end.
Comments
Post a Comment