• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

While Loop Problem

 
Jason Newman
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 6318
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct, this *is* an infinite loop. Tell my why you think it is though.
 
Jason Newman
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 6318
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 6318
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith. Please edit your post.

Thanks Paul.
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[deleted]
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swaraj gupta wrote: . . .



Never be a fishing instructor.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic