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.
if...doSomething?