Question

the last thing I want to minimize today is code that contains while and do-while loops. Here is the original:

class Vereinfache3_edit { 

        public static void main(String [] args) {

           int c1 = Integer.parseInt(args[0]) ;
           int c2 = Integer.parseInt(args[1]) ;
           int c3 = Integer.parseInt(args[2]) ;

/*  1 */           c1 += 7 ; 
/*  2 */           System.out.println( c1 ) ; 

/*  3 */       while (c1 % 8 != 0)
/*  4 */              if ( c1 % 16 == 0 ) ; 
/*  5 */              else
/*  6 */         do 
/*  7 */                 {
/*  8 */                    c1 += 7 ; 
/*  9 */                    System.out.println( c1 ) ; 
/* 10 */                    if ( c2 < c3 )
/* 11 */                       { c1 = c1+c1 ; 
/* 12 */                         c3 ++ ; 
/* 13 */                         c1 /= 2 ; 
/* 14 */                         c3 -= 1 ; 
/* 15 */                       }
/* 16 */                 }
/* 17 */                 while ( c1 % 8 != 0 ) ;

/* 18 */           c1 += 7 ; 
/* 19 */           System.out.println( c1 ) ; 
        }          

}  // end of class Vereinfache3

And here is my minimized version:

class Vereinfache3 { 

        public static void main(String [] args) {

               int c1 = Integer.parseInt(args[0]);
               int c2 = Integer.parseInt(args[1]) ;
               int c3 = Integer.parseInt(args[2]) ;

               do{
                   c1 += 7 ; 
                   System.out.println( c1 ) ;               
               }while (c1 % 8 != 0);

/* 18 */       c1 += 7 ; 
/* 19 */       System.out.println( c1 ) ; 
        }          

}  // end of class Vereinfache3

It generates the same output for me. Do you see any errors or things that can be improved?

Especially this code seems to be tricky:

/*  3 */       while (c1 % 8 != 0)
/*  4 */              if ( c1 % 16 == 0 ) ; 
/*  5 */              else
/*  6 */          do{}

How do I deal with the while? What contains the while loop?

Was it helpful?

Solution

Your minimized version produces the same result as the original version, different outputs for your original solution and your minimized version are possible only in the case when c1 % 8 != 0 and c1 % 16 == 0, this case is impossible since all multiples of 16 are multiples 8 too..

Removing the unused declarations..is the only improvement I could suggest. I do have a solution with less code..Not that it makes things any better...i don't like too many sysouts in code..:)

    int finalBound = ((c1 + 7) % 8 == 0) ? c1 + 7 : 8 * c1;
    for (; c1 <= finalBound; c1 += 7) {
        System.out.println(c1 + 7);
    }

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top