It seems like an endless loop ends if System.out.println is not used

I had a simple bit of code that was a supposed infinite loop, since x will always grow and there will always be more than j .

 int x = 5; int y = 9; for (int j = 0; j < x; j++) { x = x + y; } System.out.println(y); 

but as it is, it prints y and does not work indefinitely. I canโ€™t understand why. However, when I configure the code as follows:

 int x = 5; int y = 9; for (int j = 0; j < x; j++) { x = x + y; System.out.println(y); } System.out.println(y); 

It becomes an endless cycle, and I have no idea why. Java recognizes its infinite loop and skips it in the first situation, but does it need to execute the method call in the second so that it behaves as expected? Confused :)

+82
java for-loop infinite-loop
Feb 01 '17 at 4:49 on
source share
6 answers

Both examples are not endless.

The problem is limiting the int type in Java (or almost any other common language). When the value of x reaches 0x7fffffff , adding any positive value will overflow, and x will become negative, so below j .

The difference between the first and second loops is that the internal code takes much longer, and it will probably take several minutes before the x overflow. In the first example, this may take less than a second or, most likely, the code will be deleted by the optimizer, since it has no effect.

As mentioned in the discussion, the time will greatly depend on how the OS buffers the output, whether it outputs to the terminal emulator, etc., so it can be much higher than a few minutes.

+155
Feb 01 '17 at 4:59 on
source share

Since they are declared as int, as soon as it reaches the maximum value, the loop will be interrupted, since the value of x will become negative.

But when System.out.println is added to the loop, the execution speed becomes visible (since output to the console slows down the execution speed). However, if you allow the 2nd program (the one that has syso inside the loop) to work long enough, it should have the same behavior as the first (one without siso inside the loop).

+33
Feb 01 '17 at 5:01
source share

There can be two reasons for this:

  • Java optimizes the for loop and since x is not used after the loop, it simply removes the loop. You can verify this by setting the statement System.out.println(x); after the cycle.

  • It is possible that Java does not actually optimize the loop, and it runs the program correctly, and ultimately x will be too large for int and overflow. An integer overflow is likely to make the integer x negative, which will be less than j, and therefore it will exit the loop and print the value of y . This can also be verified by adding System.out.println(x); after the cycle.

In addition, even in the first case, an overflow will occur, which will lead to the second case, so it will never become a true endless cycle.

+13
Feb 01 '17 at 5:00
source share

They are both not infinite loops, initially j = 0, while j <x, j increases (j ++), and j is an integer so the loop will work until it reaches its maximum value and then overflow (An Integer Overflow is a condition that occurs when the result of an arithmetic operation, such as multiplication or addition, exceeds the maximum size of the integer type used to store it.), For the second example, the system simply prints the y value until the cycle breaks.

If you are looking for an example of an infinite loop, this should look like this.

 int x = 6; for (int i = 0; x < 10; i++) { System.out.println("Still Looping"); } 

because (x) will never reach 10;

you can also create an infinite loop with a double for loop:

 int i ; for (i = 0; i <= 10; i++) { for (i = 0; i <= 5; i++){ System.out.println("Repeat"); } } 

this cycle is infinite, because the first for the cycle says i <10, which is true, so that it goes into the second cycle, and the second for the cycle increases the value (i) until it is equal to 5. Then it goes to the first for the loop again, since I <10, the process continues to repeat because it is reset after the second cycle

+2
Feb 03 '17 at 11:34 on
source share

This is the final loop, because as soon as the value of x exceeds 2,147,483,647 (which is the maximum value of int ), x will become larger and not larger than j , regardless of whether print y or not.

You can simply change the y value to 100000 and type y in the loop, and the loop will break very soon.

The reason you feel it has become infinite is because System.out.println(y); made the code run much slower than without any action.

+1
Feb 08 '17 at 2:22 on
source share

An interesting problem. In fact, in both cases, the cycle is not infinite.

But the main difference between them is when it will end and how long x will take the value of max int , which is 2,147,483,647 , after which it will reach the overflow state and the cycle will end.

The best way to understand this problem is to check a simple example and save its results.

An example :

 for(int i = 10; i > 0; i++) {} System.out.println("finished!"); 

Output:

 finished! BUILD SUCCESSFUL (total time: 0 seconds) 

After testing this endless loop, it will take less than 1 second to complete.

 for(int i = 10; i > 0; i++) { System.out.println("infinite: " + i); } System.out.println("finished!"); 

Output:

 infinite: 314572809 infinite: 314572810 infinite: 314572811 . . . infinite: 2147483644 infinite: 2147483645 infinite: 2147483646 infinite: 2147483647 finished! BUILD SUCCESSFUL (total time: 486 minutes 25 seconds) 

In this case, you will notice a huge difference in the time spent on the termination and completion of the program.

If you do not have patience, you will think that this cycle is endless and will not be completed, but in fact, it will take from several hours to i to complete and reach the overflow state.

Finally, we finished after we put the print statement inside the for loop, which in the first case would take much longer than the loop without the print instruction. A.

The time taken to execute the program depends on your technical specifications of the computer, in particular on the processor power (processor), the operating system, and your development environment that compiles the program.

I check this case for:

Lenovo 2.7 GHz Intel Core i5

OS: Windows 8.1 64x

IDE: NetBeans 8.2

It takes about 8 hours (486 minutes) to complete the program.

You may also notice that the step step in the for i = i + 1 loop is a very slow factor to achieve max int value.

We can change this coefficient and take a step with a step faster to check the cycle in less time.

if we put i = i * 10 and test it:

 for(int i = 10; i > 0; i*=10) { System.out.println("infinite: " + i); } System.out.println("finished!"); 

Output:

 infinite: 100000 infinite: 1000000 infinite: 10000000 infinite: 100000000 infinite: 1000000000 infinite: 1410065408 infinite: 1215752192 finished! BUILD SUCCESSFUL (total time: 0 seconds) 

As you can see, this is very fast compared to the previous cycle.

less than 1 second is required to complete and complete the program.

After this test case, I think he should clarify the problem and prove the reliability of the answer of Zbynek Vyskovsky - kvr000 , and this will be the answer to this question .

0
Jun 05 '17 at 19:43 on
source share