I'm trying to prevent against the "Divide by Zero Exception". This code just locks up the GUI of my program for some reason :
Goal 1 : Have a whole number division.
Goal 2 : Avoid the "Divide by 0 exception".
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Check that the condition checking for one and two to be zero inside the while (the while with the '%' operator) will never be satisfied since you have already checked that in the preceding two while loops..
I posted it in my code and tried it out. It now works beautifully. Now, I am trying to understand it. I guess I'm wondering how this is different from my code... They look exactly the same to me, except for the do instead of the brackets (even though the brackets are there). How does this work differently than my previous code? Does the inside execute first?
Thanks for the help,
EDIT : "A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time."
^^ Does this have anything to do with it?
EDIT (2) : So, based on my own observations and from the quote above, I'm understanding that the do-while statements execute each time before the main while statement does. This is why it succeeds.
Joined: May 31, 2011
The do while guarantees to run at least once and hence the one and two are assigned a new value till one gets perfectly divided by two (one % two == 0). Please try to print the values of one and two after the while loop finishes. Since you previously checked whether one and two are zeros in a while loop, which is already been checked the program falls into an infinite loop as the values of one and two never changes.
do-while statements execute each time before the main while statement does
I did not understand the above question... which is a main while? The one which comes with do?
I'm glad you posted the other one. I've never used a do-while loop and never knew why it was needed. While Campbell's answer was more relevant to my question, I thank you for showing me this because A. you did give me the right answer and B. you taught me something new that otherwise, I probably wouldn't have learned.
Joined: Oct 13, 2005
That shows how useful forum discussion can be. You get lots of useful ideas, even if they are tangential to your original question.