Why doesn't my loop work?
A while loop means "at the start of each iteration of the loop, check whether the while statement evaluates to true. If it is, continue to loop, but if it isn't then stop looping and go to the next statement". Because you set retry to 0 (~= false) at the beginning of the loop, when it checks the value of retry at the start of the first pass, it is 0 ~= false, so it skips the loop. In the second variant of your code, you set it to 1 (~= true), so it runs the loop. When you want to exit the loop, you set it to 0 (~= false), so the next time it checks, it's falsy, so it skips the loop and moves onto the next code.
In C-like pseudocode, your original while loop would look like this
bool retry = false;loopstart: if (retry == true) { //do whatever was in your loop } goto loopstart
hopefully that makes it a bit more obvious that it will skip the execution of the loop.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now