1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java infinite loop (problem statement)

  1. Oct 26, 2011 #1
    1. The problem statement, all variables and given/known data

    http://pastebin.com/7ehbwkvE [Broken]

    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.

    2. Relevant equations



    3. 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 dont know what/if that sequence is convergent at all...

    i changed the while (term < 5000) and it ended fine as expected. . . i dont even know why i made this topic after all lol
     
    Last edited by a moderator: May 5, 2017
  2. jcsd
  3. Oct 26, 2011 #2

    Mark44

    Staff: Mentor

    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 (Text):
     and
    [/noparse] tags around it.
    Code (Text):

    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: May 5, 2017
  4. Oct 26, 2011 #3
    yeah, i saw that after, but still used pastebin, because of the syntax highlighting. . .

    our professor doesnt 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.
     
  5. Oct 26, 2011 #4

    Mark44

    Staff: Mentor

    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.
     
  6. Oct 26, 2011 #5
    gotcha, thanks.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java infinite loop (problem statement)
  1. Java problem (Replies: 55)

  2. Java ending a loop (Replies: 2)

Loading...