Do Loop
Previous    Next    Home    Source    Package

Notes
  1. Syntax of a do expression:
    do { <body> } while ( <condition> ) 
    
  2. The do loop acts the same way as the while loop except that the test of the <condition> occurs after an iteration of the loop is complete. This sometimes saves some code. For example
       /* Guilty until proven innocent */
       good_enough = false;
    
       do {
          n = new BigInteger(size,quality,sr);
          x = n.subtract(ONE).divide(TWO);
    
          if(x.isProbablePrime(quality)) 
             good_enough = true;
    	    
          System.out.print(++counter + " " );	
       } while (!good_enough);
    
    and
       /* Guilty until proven innocent */
       good_enough = false;
    
       n = new BigInteger(size,quality,sr);
       x = n.subtract(ONE).divide(TWO);
    
       if (x.isProbablePrime(quality))
          good_enough = true;
    	    
       System.out.print(++counter + " " );	
    
       while (!good_enough) {
          n = new BigInteger(size,quality,sr);
          x = n.subtract(ONE).divide(TWO);
    
          if(x.isProbablePrime(quality))
             good_enough = true;
    	    
          System.out.print(++counter + " " );	
       }
    
    do the same thing. Hopefully we will get to explain the code above later during the camp.