# My "while" method is acting funny.

Joseph Alrawi
Greenhorn
Posts: 23
I'm currently reading a book for beginners in java. And I stumbled onto a problem when I wanted to make a code for learning purposes, combining things such as do, for, while etc etc. The code is long, but the important parts are:

and

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?

Thanks for your time and help!

Henry Wong
author
Marshal
Posts: 21515
84
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

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65229
95
Aside: while is a statement, not a method.

Joseph Alrawi
Greenhorn
Posts: 23
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

Hello Henry! Thanks for your help, I tried inserting:

And it seems to be working fine now, just with a few remaining errors I need to fix. My solution was mostly a guess, until I finally understood what you meant, thank you!

Campbell Ritchie
Sheriff
Posts: 50277
80
Joseph Alrawi wrote: . . . My solution was mostly a guess . . .
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.
If you think about the problem and plan it, there is a good chance your first attempt will work

Campbell Ritchie
Sheriff
Posts: 50277
80
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
Greenhorn
Posts: 23
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.

Hey.

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
author
Marshal
Posts: 21515
84
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

Joseph Alrawi
Greenhorn
Posts: 23
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

But I want to keep the diceNO for later use. Changing it in my loop will mean that it will take on a new value, that's why I assigned another variable called diceNO2 which takes care of that problem for me. It just felt simpler, what I know is that what stays in the "for" loop does not come out, meaning the diceNO would be unchanged after the loop has finished right? But despite that I need the original value of diceNO in order for the loop to work, otherwise the loop will detect the current value of diceNO, meaning the loop wouldn't work as I wanted.

Campbell Ritchie
Sheriff
Posts: 50277
80
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.

Joseph Alrawi
Greenhorn
Posts: 23
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.

I'm definitely not saying I know more than you, god no. It's just that, I usually work on my own, programming is more of a fun thing for me. I know you have more experience than me, I just prefer to work it my own way, and finally realizing what is right or wrong. It might happen that some people might not be able to help in that case, but that is something I got to live with I guess.

Jake Augustine
Greenhorn
Posts: 9
1
I don't think anyone was trying to compare their knowledge to yours, but rather they're just trying to say that your code wasn't working initially, so you kept making changes and adding on to it until it has become so complicated that it's hard for anyone else trying to read it to understand it, even if they are an experienced programmer, and that's probably adding to your problem of not being able to get it to work the way you want. It's not too hard for you to follow what you're trying to do because it's your code. I think that is why you were given the advice to erase it, write down what you want it to do, then write the code out. Think of it like you are getting ready to build a house (or anything for that matter). What would be a better plan? To just have a
general idea of what you want, then go right to building it? Or would it be better to think of everything you want, come up with a detailed plan, then build it with the instructions that you've set up for yourself?
I haven't been doing programming for very long, and just like you, this is something that I want to do for fun (at least right now, but who knows what will happen after I gain more experience with Java and move on to other languages ), but don't you want to learn the best possible ways of doing it, not only to be able to learn faster, but to also get more satisfaction out of it. I ended up copying your code to a text editor and playing around with it (it's something I do when I need a break from the books and it gives me more practice) and I did get it to work. It's not really that hard, but I think you're so focused on it that you have developed what we used to refer to in the Navy as "tunnel-vision". Take a step back and try looking at the first line of your For Loop with a fresh set of eyes. Do you see anything that could be changed? Say the user chooses 2 dice (I'm assuming DiceNO is the number of dice, which would be easier to understand written as numOfDice). So with 2 dice you go through the For Loop the first time with DiceNO2 being a value of 2, then it becomes a value of 1 as it goes through it again. But take a look at the boolean logic part (not sure if that's what it's called or not)...DiceNO2 >= 0. That means it runs through the for loop a third time, causing 3 rolls with only 2 dice. I'm sure that if you were to change that part of the for loop, it would help you make the rest of the entire while statement simpler, i.e. not needing that second If statement. Good luck and have fun. I'm sure you'll figure it out

Campbell Ritchie
Sheriff
Posts: 50277
80
Far better explained than I would have managed it. Thank you and have a cow

If you want to run a while loop a predetermined number of times you can do this:-… but for such counter‑controlled repetition it is commoner to use a for loop:-

Jake Augustine
Greenhorn
Posts: 9
1
Yay, my first cow! Thanks!!