# While Loop Problem

Jason Newman
Ranch Hand
Posts: 53
I seem to being having a problem with this code.

Yes, it is an infinite loop. But, I have NO idea how to fix it. I was taught loops, and I can recognize an infinite loop, but I have no idea how to fix one. So please, any help would be greatly appreciated.

Stephan van Hulst
Bartender
Posts: 6471
83
You are correct, this *is* an infinite loop. Tell my why you think it is though.

Jason Newman
Ranch Hand
Posts: 53
Stephan van Hulst wrote:You are correct, this *is* an infinite loop. Tell my why you think it is though.
.

Well, for one the interactions pane was taking far too long to run it. And I'm not sure if this is right, but I think you have to have some way to declare that the loop needs to stop. Which, I thought I accomplished this with: . I thought I said "Give me all multiples of three less than or equal to 1,000. But it looks like I'm wrong. :/

Paul Clapham
Sheriff
Posts: 21443
33
Yes, that's one of the classic beginner blunders. To find it you need to go through a process called "debugging", where you find out what lines of code are and are not being executed. (Have you not heard of that process yet?) The quick and dirty way to do that is to insert lines like

and see what they do when you run the code.

Jason Newman
Ranch Hand
Posts: 53
Ah. I have heard of debugging. But, that's as far as it goes. I know nothing more than it is something code related.

Stephan van Hulst
Bartender
Posts: 6471
83
You simply insert pieces of code to find out if your actual program does what you expect it to do.

I'll give you a hint. Insert "System.out.println("Outside the loop");" right before the while statement, and insert "System.out.println("Inside the loop");" right before "total = total + num;".

First, think about what you would expect the code to do. Then look what your code actually prints.

Keith Williams
Greenhorn
Posts: 2
This is not an infinite loop. I believe it is a logic error. [Spoiler removed -- Paul C -- please don't be insulted, Keith, you are correct, but there's a teachable moment going on here.]

Stephan van Hulst
Bartender
Posts: 6471
83

Thanks Paul.

Alex Hurtt
Ranch Hand
Posts: 98
Here's a hint. Answer the following question and you'll probably have a facepalm moment...what is the function of the semi-colon in Java?

swaraj gupta
Ranch Hand
Posts: 182
[deleted]

Alex Hurtt
Ranch Hand
Posts: 98
swaraj gupta wrote: . . .

Never be a fishing instructor.

Campbell Ritchie
Sheriff
Posts: 50644
83
Swaraj Gupta, please read this, which is at the start of the Beginning Java™ forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Somebody has already had a post removed because it violated this suggestion, and you go and repeat the error. By giving such an answer, you are depriving Jason Newman of the opportunity to learn. I am pulling rank and deleting your post and altering the "don't be a fishing instructor" quote in the hope that Jason Newman will have the chance to learn.

swaraj gupta
Ranch Hand
Posts: 182
Ok.. Well i am new to this community and not aware of such things..from the next time onwards i do, don't worry...

Campbell Ritchie wrote:Swaraj Gupta, please read this, which is at the start of the Beginning Java™ forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Somebody has already had a post removed because it violated this suggestion, and you go and repeat the error. By giving such an answer, you are depriving Jason Newman of the opportunity to learn. I am pulling rank and deleting your post and altering the "don't be a fishing instructor" quote in the hope that Jason Newman will have the chance to learn.

Jason Newman
Ranch Hand
Posts: 53
Let me start off by saying: Thank you for all the help. I removed the semi-colon from the end of the while loop, and I believed that fixed it. This is a GREAT website! Thank you again!

Campbell Ritchie
Sheriff
Posts: 50644
83
• 1
Now you have found the solution to your problem, I shall no longer have to delete posts saying "remove the semicolon." You will remember that pitfall next time it happens, I am sure.

What is happening, is you are initialising a loop, then going into the semicolon. The JVM will interpret that as "while xxxx do whatever follows". And "whatever follows" is nothing followed by a semicolon. So the JVM will repeatedly do nothing, find the loop ocntinuation condition is still true, do nothing, find the loop continuation, etc etc ad infinitum. So you get a tiny little loop consisting of a ; which never terminates. It is possible to have a loop with no body; when Java was first developed, its users came from a C background, where they like doing that sort of thing. This loop will summate the numbers from 0 to 10 inclusiveYou can try it and see it works, and also how difficult it is to read. The ; at the end is read by the JVM as "do nothing", but there is an i++ in the () after for, so that loop will terminate.

Jason Newman
Ranch Hand
Posts: 53
Huh, thats pretty confusing. Sorry, I'm a slow learner. The method didn't accomplish the task I thought it would, or at least what I tried to make it do, so it is now back to square one. What I was trying to do is get the sum of all the multiples of three, below or equal to 1,000. Any chance I could get some more help...?

fred rosenberger
lowercase baba
Bartender
Posts: 12228
36
We are happy to help...but you have to show us your code in order for us to have any clue what to look at.

What Campbell was saying was that when you had this:

you have two things - a while-loop that ends at the semi-colon, and a block of code defined by the brackets. The way you wrote your while-loop, it would never exit. Campbell was then illustrating that there are cases where a loop with no body is perfectly valid and gave an example.

Campbell Ritchie
Sheriff
Posts: 50644
83
fred rosenberger wrote:. . . Campbell was then illustrating that there are cases where a loop with no body is perfectly valid and gave an example.
I also said it was difficult to read. I didn't expect you to understand it. That little example, which I had to correct several times before it worked, shows how confusing an loop with an empty body can be.

Campbell Ritchie
Sheriff
Posts: 50644
83
Jason Newman wrote:. . . The method didn't accomplish the task I thought it would, . . .
It calculated the correct answer when I tried it, minus the semicolon.

Jason Newman
Ranch Hand
Posts: 53
So, I DID indeed write a method that accumulated the sum of all the multiples of three, below or equal to 1,000? I feel like I'm learning.

And thank you Fred, that helped explain it a lot.