Java infinite loop (problem statement)

  • Context: Comp Sci 
  • Thread starter Thread starter Highway
  • Start date Start date
  • Tags Tags
    Infinite Java Loop
Click For Summary

Discussion Overview

The discussion revolves around a Java programming problem involving an infinite loop. Participants are tasked with identifying and fixing the issue in the provided code, which is intended to calculate a sum but fails to terminate correctly. The conversation includes aspects of homework-related programming, technical explanation, and conceptual clarification.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants note that the infinite loop is caused by a divergent sequence that does not satisfy the loop's termination condition.
  • One participant suggests changing the condition to "while (term < 5000)" as a potential fix.
  • Another participant points out that the variable 'term' is defined as an integer, questioning the logic of checking if it is greater than 0.0001.
  • There are comments regarding the vagueness of the problem statement provided by the professor, with some expressing frustration over unclear instructions and errors in the code given as a basis.
  • Participants discuss the importance of posting code directly in the forum rather than using external links for clarity and ease of feedback.

Areas of Agreement / Disagreement

Participants express frustration over the problem statement and the provided code, indicating a lack of consensus on the clarity of the assignment. There is no agreement on a definitive solution to the infinite loop issue, as multiple suggestions are offered without resolution.

Contextual Notes

Limitations include the ambiguity of the problem statement, the potential misunderstanding of variable types, and the unresolved nature of the infinite loop issue. The discussion reflects varying levels of understanding and approaches to the problem.

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 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K