-2
for(int j=0;j<10;j++){
        if (xx2==0){
            f =xx1*(Math.pow(x,p))+xx3;
            derF =   xx1*(p*(Math.pow(x,(p-1)))); 
            get3=x-(f/derF);
            Arr[j+1]=get3;
            String roundXn = df3.format(Arr[j+1]);
            String roundfxprime = df3.format(derF);
            String roundfx = df3.format(f);
            if (Arr[j]==Arr[j+1])
            {
                break;
            } 


            //x=x2;
            System.out.println("\n" + j+"\t" + x +"\t" + roundfx +"\t" +roundfxprime +"\t" +roundXn); 
            //roundXn = Convert.parseInt(
            Double Xn1= Double.parseDouble(roundXn);

            x = Xn1;

Code result:

screenshot

I want the loop to stop at 3rd iteration, but the loop still continues. How do I stop it?

  • 1
    What is xx2 ? First, read how to ask a question and then I can help – FallAndLearn Apr 07 '16 at 07:18
  • 2
    Maybe `j < 3` in the outer for loop declaration? – Andy Turner Apr 07 '16 at 07:19
  • Hi randy and welcome to stackoverflow! Your question seems to be a bit confusing. Surf over to http://stackoverflow.com/help/how-to-ask and check out how to ask a question to get achieve a high chance of getting an answer to your question. – sics Apr 07 '16 at 07:20
  • In the output. The iteration still continues.. Its supposed to stop at 3 iteration. – Randy Corpuz Apr 07 '16 at 07:25
  • @FallAndLearn xx2 is a user input. If he input 0 the condition is executed – Randy Corpuz Apr 07 '16 at 07:27
  • @sics. Sorry about that. Its my first time to ask here.. – Randy Corpuz Apr 07 '16 at 07:28
  • 3rd Iteration is only valid for this case right? Other params may result in different count of iterations if I understand correctly. So you'll have to think about what exactly shall be the criteria to break the loop.Is it when Fx == Xn+1 ? If so, then you have to check for that condition. – Fildor Apr 07 '16 at 08:16
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Apr 07 '16 at 08:50
  • @fildor the iteration is suppsoed to stop if the Xn+1 has the same result. – Randy Corpuz Apr 07 '16 at 09:01
  • There you go ... `if( x == Xn1 ) break; else x = Xn1;` ? – Fildor Apr 07 '16 at 09:59

1 Answers1

0

use

for(int j=0;j<3;j++)

or add a check in place where you need (maybe before if (xx2==0){)

if(j==2) break;

You can exit any lopp in java by using break;

Alex Torson
  • 289
  • 4
  • 13