endless loop

Keywords: Java

Dead cycle

  • A dead cycle is a cycle that cannot be ended. (endless loop / infinite loop)
  • The reason for the occurrence of the dead cycle is that the end condition is not set properly. The end condition of the cycle is very important, and various boundary conditions should be fully considered.

For example, the exercise in the above essay (find n numbers that can be divided by integer). If there is no condition of cumulative times, the condition expression will always be satisfied, and the program will always execute. This creates a dead cycle.

public class FindDivEndless {
    public static void main(String[] args) {
        int n = 5;
        int dividend = 100;
        int divisor = 89;

        int found = 0;

        while(found<n) {
            if(dividend%divisor == 0) {
                System.out.println(dividend + "Can be" + divisor + "To be divisible by. Shang Wei" + (dividend/divisor));
            }
            dividend++;
        }
    }
}

 

A special example

  • Use while to find 5 numbers divisible by 200000
  • The process will end eventually
public class FindNDivNotEndless {
    public static void main(String[] args) {
        int n = 5;

        int dividend = 100;
        int divisor = 2000000000; // Value will overflow int Value range of

        int found = 0;

        while (found < n) {

            if (dividend % divisor == 0) {
                found++;
                System.out.println(dividend + "Can be" + divisor + "To be divisible by. Shang Wei" + (dividend / divisor));
            }

            dividend++;
        }
    }
}

 

The reason for this is that 2 billion is close to the maximum value of int, and further accumulation will lead to numerical overflow.

According to the addition of binary, the highest bit of the addition will be 1. In the computer, the binary value is represented and stored in the form of complement,

So when the highest sign bit is 1, it becomes a negative number, which is why the second number found is a negative number.

So, if you don't just find five numbers that can be divided by integers, you will keep repeating 1, - 1, 0, 1, and - 1.

So how to solve the problem of negative result caused by numerical overflow?

 

Use break statement to end loop

  • A break statement can end any loop
  • Use break to solve the problem without considering the negative number
public class FindNDivBetter {
    public static void main(String[] args) {
        int n = 5;

        int dividend = 100;
        int divisor = 2000000000;

        int found = 0;

        String start = "from" + dividend + "At first,";

        while (found < n) {
            // When the dividend value overflows, jump out of the whole while Cycle.
            if (dividend < 0) {
                System.out.println("Dividend overflow, end of calculation!");
                break;
            }

            if (dividend % divisor == 0) {
                found++;
                System.out.println(dividend + "Can be" + divisor + "To be divisible by. Shang Wei" + (dividend / divisor));
            }

            dividend++;
        }

        System.out.println(start + "Find out" + found + "One can be" + divisor + "The number of divisions.");
        System.out.println(dividend); // The result is-2147483648,It's really a negative number.
    }
}

Posted by facets on Tue, 07 Jan 2020 02:23:46 -0800