Joseph Alrawi wrote:
I've been working with it for a LONG time now, and I really can't seem to get this stage of the program right. The problem is, despite total being equal to sum1, it still loops, which it shouldn't. What have I done wrong?
Henry Wong wrote:
Joseph Alrawi wrote:
I've been working with it for a LONG time now, and I really can't seem to get this stage of the program right. The problem is, despite total being equal to sum1, it still loops, which it shouldn't. What have I done wrong?
How do you know that total is equal to sum1? Are you using the printout that print the value of total and sum1? The printout right before you set total back to zero?
The while loop doesn't magically terminate when the expression is satisfied. It only checks when the body finishes, and it has to decide whether to do another iteration of the loop or not.
Henry
You can tell. Guessing will work; if you make a million guesses, all different, there is a chance that one of them will be correct. You only need to test a million options.Joseph Alrawi wrote: . . . My solution was mostly a guess . . .
Campbell Ritchie wrote:I still think that checking whether diceNO is 0 looks a bad way to design a loop. Ask whoever is sitting next to you to go through that loop and work out how it works. If they can do that quickly then the loop is well‑written.
Joseph Alrawi wrote:
The problem is I'm not working with anyone, I'm just reading a book by myself. Since the programming class already had too many students I decided to do it myself. When you say that diceNO being 0 is a bad way of designing a loop, I call diceNO earlier in the code, the user types any value that the user wants. That was my main concept of the program. I'm not sure what you mean about diceNO being 0, diceNO has a value to begin with, then diceNO2 uses the value from diceNO to make an acceptable loop.
Henry Wong wrote:
Joseph Alrawi wrote:
The problem is I'm not working with anyone, I'm just reading a book by myself. Since the programming class already had too many students I decided to do it myself. When you say that diceNO being 0 is a bad way of designing a loop, I call diceNO earlier in the code, the user types any value that the user wants. That was my main concept of the program. I'm not sure what you mean about diceNO being 0, diceNO has a value to begin with, then diceNO2 uses the value from diceNO to make an acceptable loop.
Since this topic has moved into suggestions / code review -ish mode ... The first thing that jumps out and bothers me with the loop is the use of "diceNO" and "diceNO2".
Granted that you can guess the meaning of diceNO as the number on the dice, but what is diceNO2? It is a variable used by the loop, and a "2" tacked on doesn't explain that (nor what it does). What really bothers me though, is how close the two variables are. It is really easy to use diceNO instead of diceNO2, and vica versa, and not notice it -- especially when you are staring at the code debugging for a few hours.
Henry
Campbell Ritchie wrote:Imagine I am sitting next to you and you have asked me to understand your loop. I have over ten years' programming experience and I can't understand it just by looking.
I think you will actually have to delete the entire loop.
Now, write down very carefully what it is supposed to do. I think it is much simpler than you think. Write down in plain English what the loop is supposed to do, and don't write anything which looks even slightly like Java code. Then let us look at it.