Infinite loop (endless loop)

Endless loop

  • Death cycle is a not the end of the cycle . (endless loop / infinite loop)
  • Infinite loop occurs because there is no set end condition , end condition for the loop is very important to fully consider the various border situations .

An essay in the above exercises (find n number can be divisible by number), for example, if the lack of cumulative number of conditions, it will make a conditional expression never meet , so that the program will never executed . This will produce an endless loop.

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 + "可以被" + divisor + "整除。商为" + (dividend/divisor));
            }
            dividend++;
        }
    }
}

 

A special case

  • A while to find the five number can be divisible by 2 billion
  • The final program will still end
public  class FindNDivNotEndless {
     public  static  void main (String [] args) {
         int n-=. 5 ; 

        int dividend The = 100 ;
         int divisor = 2000000000.; // numerical overflow in the range of int 

        int found = 0 ; 

        the while (found < n- ) { 

            IF (% dividend the divisor == 0 ) { 
                found ++ ; 
                System.out.println (dividend the + "may be" + divisor + "is divisible List." + (dividend the / divisor)); 
            } 

            dividend the ++ ;
        }
    }
}

 

 The reason for this is that 2 billion close to the maximum int value, and then down the accumulated value will cause an overflow.

Addition in binary, then add the plus the most significant bit will be 1, and in the computer, and the stored binary value is represented by complement form,

Therefore, the highest sign bit is 1, it becomes a negative number, which is why the number of the second reason is found negative.

Thus, if only to find 5 may be divisible, we will continue to 1, -1, 0, 1, -1 This is repeated down.

So how to solve the problem of numerical overflow in the negative result of it?

 

Use the break statement ending cycle

  • break statement to terminate any loop
  • Without considering the negative case, break solve problems
public  class FindNDivBetter {
     public  static  void main (String [] args) {
         int n-=. 5 ; 

        int dividend The = 100 ;
         int divisor = 2000000000. ; 

        int found = 0 ; 

        String Start = "from" + dividend + "Start," ; 

        the while (found < n-) {
             // when the dividend value overflows, the entire out while loop. 
            IF (dividend The <0 ) { 
                System.out.println ( "dividend overflow calculate the end!" );
                 BREAK ; 
            } 

            IF(% dividend The divisor == 0 ) { 
                found ++ ; 
                System.out.println (dividend The + "may be" + divisor + + (dividend / " is divisible List." divisor)); 
            } 

            dividend The ++ ; 
        } 

        the System. out.println (Start + "total" + found + "can be a" + divisor + "divisible." ); 
        System.out.println (dividend the); // result is -2147483648, indeed a negative number. 
    } 
}

Guess you like

Origin www.cnblogs.com/buildnewhomeland/p/12158384.html