0

I have this piece of Code in which there are two loops 'i' and 'j'.

'j' is the inner loop variable and it supposed to run 999 to 100 on single run of outer loop.

but it runs randomly like, suppose i=999

j=912
j=911
j=910
j=909
j=908
j=907
j=906

Then suddenly inner loop quits, decrements by 1 from outer loop and makes i=998

then start 'j' loop

j=908
j=907
j=906
j=905
j=906
j=905
j=904
j=903
j=902

then quits inner loop ................

    int product=0;
    mainloop:
    for(int i=999;i>99;i--){
        for(int j=999;j>99;j--){
            boolean flag= doSomething(i*j);
            if(flag){
                product=i*j;
                System.out.println("Digits are: "+i+" and "+j);
                break mainloop;
            }
        }
    }

 public  boolean doSomething(int product){
        String original= Integer.toString(product),reverse="";
        int length = original.length();
        for ( int i = length - 1; i >= 0; i-- )
            reverse = reverse + original.charAt(i);
        return (original.equals(reverse));
            
        
    }

why is it happening? why inner loop doesnot complete cycle from 999 to 100?

EDIT: To clear, 'flag' will be true only if j=913 and i=993 , this is the main issue that loop doesn't break at this point because 'j' never comes to 913 but it randomly generates number.Moreover 'break' will break the mainloop not only the inner loop.

3
  • look at the code in doSomething. that should clear it up Commented Jan 13, 2015 at 7:51
  • You are explicitly breaking out of it and back to the main loop at the end of the if... Commented Jan 13, 2015 at 7:52
  • Can you show the code of doSomething? Commented Jan 13, 2015 at 8:13

1 Answer 1

3

You have a condition in the inner loop - if(flag) - which, if true, breaks from that loop. That's the reason the inner loop doesn't complete. It doesn't run "randomly". Its behavior depends on the result of the doSomething(i*j) method call.

Running your code with some additional debug prints and the implementation of doSomething you claim to have :

  int product=0;
  mainloop:
  for(int i=999;i>99;i--){
    System.out.print ("\n"+i+":");
    for(int j=999;j>99;j--){
      System.out.print (j+",");
      boolean flag= doSomething(i*j);
      if(flag){
        product=i*j;
        System.out.println("\nDigits are: "+i+" and "+j);
        break mainloop;
      }
    }
  }

  public static boolean doSomething(int k)
  {
      return k == 913*993;
  }

gives the expected output:

999:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
998:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
997:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
996:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
995:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
994:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,
993:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,979,978,977,976,975,974,973,972,971,970,969,968,967,966,965,964,963,962,961,960,959,958,957,956,955,954,953,952,951,950,949,948,947,946,945,944,943,942,941,940,939,938,937,936,935,934,933,932,931,930,929,928,927,926,925,924,923,922,921,920,919,918,917,916,915,914,913,
Digits are: 993 and 913

EDIT, doSomething you posted returns true when i==995 and j==583. That's when you break the loop.

Sign up to request clarification or add additional context in comments.

2 Comments

'flag' will be true only if j=913 and i=993 , this is the main issue that loop doesn't break at this point because j never comes to 913 but it randomly generates number. and 'break' will break the main loop not only the inner loop.
@HappyDev i==995 comes before i==993, so (i==995,j==583) comes before (i==993,j==913).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.