# Java infinite loop (problem statement)

• Comp Sci
• Highway
In summary, the while loop is creating a divergent sequence that will never satisfy the terms of the while loop and terminate. The code was changed to a for loop and it worked fine.

## Homework Statement

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.

## 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:
Highway said:

## Homework Statement

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.

## 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
*       (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.

## What is a Java infinite loop?

A Java infinite loop is a programming error that occurs when a loop in a Java program never terminates or ends. This can result in the program continuously running and not producing any output.

## What causes a Java infinite loop?

A Java infinite loop is usually caused by a mistake in the loop's condition or the code within the loop. If the condition is never met, the loop will continue to run indefinitely. Similarly, if the code within the loop never changes the condition, the loop will never end.

## How can I fix a Java infinite loop?

To fix a Java infinite loop, you need to identify the cause of the problem. This may involve reviewing your loop's condition and making sure it is being updated correctly within the loop. You may also need to check for any logical errors in the code within the loop.

## What are some common strategies for avoiding Java infinite loops?

One common strategy for avoiding Java infinite loops is to use a counter variable within the loop's condition, which will eventually reach a limit and cause the loop to end. Another strategy is to use a break statement within the loop to manually terminate it if a certain condition is met.

## Why is it important to avoid Java infinite loops?

Java infinite loops can cause programs to crash or freeze, which can lead to loss of data and inconvenience for users. They can also consume a lot of processing power and memory, slowing down the program and potentially causing other errors. It is important to avoid them to ensure the proper functioning of your program.