Comp Sci Java infinite loop (problem statement)

AI Thread Summary
The discussion centers around a Java code snippet that results in an infinite loop due to a poorly defined condition. The original while loop checks if a variable 'term' is less than 5000, which leads to a divergent sequence that never terminates. A suggested fix involves changing the condition to while (term > 0.0001), which allows the loop to end correctly. Participants emphasize the importance of clarity in problem statements and the need to post code directly in the forum for easier troubleshooting. The overall sentiment reflects frustration with the lack of guidance from the professor regarding the assignment.
Highway
Messages
349
Reaction score
1

Homework Statement



http://pastebin.com/7ehbwkvE

expires in 1 month...

the problem statement is very vague... we are given this code of an infinite loop, and need to fix the problem that it poses by either using a correct while loop or a for loop.

Homework Equations


The Attempt at a Solution



so looking at this and the comments given, the loop seems to be creating a divergent sequence that will never satisfy the terms of the while loop and terminate... though, i don't know what/if that sequence is convergent at all...

i changed the while (term < 5000) and it ended fine as expected. . . i don't even know why i made this topic after all lol
 
Last edited by a moderator:
Physics news on Phys.org
Highway said:

Homework Statement



http://pastebin.com/7ehbwkvE

expires in 1 month...

the problem statement is very vague... we are given this code of an infinite loop, and need to fix the problem that it poses by either using a correct while loop or a for loop.

Homework Equations





The Attempt at a Solution



so looking at this and the comments given, the loop seems to be creating a divergent sequence that will never satisfy the terms of the while loop and terminate... though, i don't know what/if that sequence is convergent at all...

i changed the while (term < 5000) and it ended fine as expected. . . i don't even know why i made this topic after all lol

Instead of posting your code at some remote site, why not just put it directly in your post, like this?

Also, when you post code here, put [noparse]
Code:
 and
[/noparse] tags around it.
Code:
class SumsLimit {

     public static void main (String args[])
    {
       double sum = 1.0 ;
       int term = 2 , x= 0; 

       /*               
        *       1-1/2+1/3-1/4..stop when Sum is approximately 0.6931.
        *       and as long as term is greater than 0.0001 
        *       your loop goes here  
        *       (a for, while or do while loop your choice)
        *       for (x = 0; x <= 5000; x++)
        *       while (term < 5000 )
        */

       while ((term >.0001)  )
       {
          System.out.println (  sum);
          System.out.println ( "\n" + x); 
          if (term % 2 == 0)
             sum = sum - (1/(double)term) ;
          else sum = sum + (1/(double)term);
          //end if
          term++;
          x++;
          System.out.println (  sum);
          System.out.println ( "\n" + x);
       } // end loop
                
    } // end main                           

} // end SumsLimit
On the offchance that you don't know why the code above is wrong, term is defined as an int, so why is it checking to see if term > .0001?
 
Last edited by a moderator:
yeah, i saw that after, but still used pastebin, because of the syntax highlighting. . .

our professor doesn't think anything through, or make a clear defined problem statement (there is nothing formally written up that we have to follow while doing a program -- it's ridiculous).

most of the code he gives us to use as a basis is full of errors and other stuff like what you saw above.
 
Highway said:
yeah, i saw that after, but still used pastebin, because of the syntax highlighting. . .
Which is really irrelevant. If you want help, make it as easy as possible on the people helping you, by posting the code directly right here. I would venture to say that some people would not want to go to the trouble of clicking a link to see the code.

Having the code here allows us to insert a comment at exactly the right point in your code, and not have to describe verbally where the problem is. Plus we don't have to deal with writing something in one window while looking at the code in another window.
 
gotcha, thanks.
 

Similar threads

Replies
12
Views
2K
Replies
3
Views
1K
Replies
1
Views
1K
Replies
9
Views
2K
Replies
3
Views
6K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
5
Views
3K
Back
Top