how to "break" from two nested for ... if loop

 

Dear Members

I have created a nested for loop shown in below in code section.

I want to exit (break) when variable idxFB = j is found. Currently it do break of inner loop on satisfying the condition but then goes to first outer loop and loop again for inner loop.

seeking advise from experts here.

for(int i = 1; i <= lookback; i++) {                            // LOOK BACK THREE FRACTALS
        int idxFractal = FractalM15_BULL[i].idx;

        for(int j = idxFractal; j <= idxFractal+2; j++) {       // LOOK BACK TWO EXTRA BARS BEFORE FRACTAL
                if(dbbM15.IsBB_OS(HAshi,j)) {
                        idxFB = j;
                        break;
                }
        }
} // End of For Loop


 

 
Anil Varma:

Dear Members

I have created a nested for loop shown in below in code section.

I want to exit (break) when variable idxFB = j is found. Currently it do break of inner loop on satisfying the condition but then goes to first outer loop and loop again for inner loop.

seeking advise from experts here.


 

You can force the first loop to end by making the condition false, playing around with the value of "i", like this:


for(int i = 1; i <= lookback; i++) {                            // LOOK BACK THREE FRACTALS
        int idxFractal = FractalM15_BULL[i].idx;

        for(int j = idxFractal; j <= idxFractal+2; j++) {       // LOOK BACK TWO EXTRA BARS BEFORE FRACTAL
                if(dbbM15.IsBB_OS(HAshi,j)) {
                        i = lookback;
                        idxFB = j;
                        break;
                }
        }
} // End of For Loop
 
Carlos Moreno Gonzalez #:

You can force the first loop to end by making the condition false, playing around with the value of "i", like this:


Thanks Carlos

Will give it a try, I thought there might be a direct way of doing it.

 
Anil Varma #:

Thanks Carlos

Will give it a try, I thought there might be a direct way of doing it.

I don't know of any other way since a break always gets out of the nearest "for"/"while", so in the outer "for" you need a second break, but you need to compare anyway. I think forcing the outer loop to end in that way is the simplest, or at least the simplest I know.

Regards.

EDIT: Maybe an improvement would be setting another condition (the one that should break from both loops) in the header of the outer loop. I haven't tried the syntax, but should work, imho. You just have to be mindful of the initial values of "idxFB" and "j", and make sense of it. I don't know your code, so can't say, but it might be worth giving it a go:

for(int i = 1; i <= lookback || idxFB == j; i++) {                            // LOOK BACK THREE FRACTALS
        int idxFractal = FractalM15_BULL[i].idx;

        for(int j = idxFractal; j <= idxFractal+2; j++) {       // LOOK BACK TWO EXTRA BARS BEFORE FRACTAL
                if(dbbM15.IsBB_OS(HAshi,j)) {
                        idxFB = j;
                        break;
                }
        }
} // End of For Loop
 
This fails (to compile) because j does not yet exist.
It is also hard to understand; what does idxFB==j mean?
You want to continue the loop through the lookback, and you have not found it. Loop currently processes all.
for(int i = 1; i <= lookback || idxFB == j; i++) {   // LOOK BACK THREE FRACTALS
   int idxFractal = FractalM15_BULL[i].idx;

   for(int j = idxFractal; j <= idxFractal+2; j++) { // LOOK BACK TWO EXTRA BARS BEFORE FRACTAL
      if(dbbM15.IsBB_OS(HAshi,j)) {
         idxFB = j;
         break;
      }
   }
} // End of For Loop
Write self-documenting code.
bool haveFound=false;
for(int i = 1; i <= lookback && !haveFound; ++i) {       // LOOK BACK THREE FRACTALS
   int idxFractal = FractalM15_BULL[i].idx;

   for(idxFB = idxFractal; idxFB <= idxFractal+2; ++idxFB) { // LOOK BACK TWO EXTRA BARS BEFORE FRACTAL
      haveFound = dbbM15.IsBB_OS(HAshi,idxFB)
      if(haveFound) break; // Found it, exit both loops.
      }
   }
} // End of For Loop
 
Carlos Moreno Gonzalez #:

I don't know of any other way since a break always gets out of the nearest "for"/"while", so in the outer "for" you need a second break, but you need to compare anyway. I think forcing the outer loop to end in that way is the simplest, or at least the simplest I know.

Regards.

EDIT: Maybe an improvement would be setting another condition (the one that should break from both loops) in the header of the outer loop. I haven't tried the syntax, but should work, imho. You just have to be mindful of the initial values of "idxFB" and "j", and make sense of it. I don't know your code, so can't say, but it might be worth giving it a go:

Thanks a lot Carlos for suggesting different ways.

I have following way and it has worked ...

This is complete code for this section.

        //+-------------------------------------------------------------------------------------------------------------------------+
        //| LOOK BACK idxFB IN FIVE BULLISH FRACTALS
        //+-------------------------------------------------------------------------------------------------------------------------+
                for(int i = 1; i <= lookback; i++) {                                                                                                                                                    // LOOK BACK THREE FRACTALS
                        int idxFractal = FractalM15_BULL[i].idx;

                        for(int j = idxFractal; j <= idxFractal+2; j++) {                                                                                               // LOOK BACK TWO EXTRA BARS BEFORE FRACTAL
                                if(dbbM15.IsBB_OS(HAshi,j)) {
                                        PrintFormat("%s: IsBB_OS[%i][%s]",(string)__FUNCTION__,j,(string)dbbM15.IsBB_OS(HAshi,j));
                                        idxFB = j;
                                        break;
                                }
                        }
                        if(idxFB == 0)          continue;
                        else                    break;
                } // End of For Loop
 
William Roeder #:
This fails (to compile) because j does not yet exist.
It is also hard to understand; what does idxFB==j mean?
You want to continue the loop through the lookback, and you have not found it. Loop currently processes all.
Write self-documenting code.

Hi William

Thanks for your response. It is similar to what I have finally done. Only problem, break inside inner loop only breaks out from inner loop and will again from outer one.

The ideas is to get idxFB (Index of First Bar for Double Bottom), Fractals usually are lower values (so I avoid searching bearish bar which is OverSold on Bollinger Band Normalised Close %BB). In real life however the exact bar of fractal may or may not be OSold. In inner loop, I have tried to look back two more bars from fractal bar.

Thanks again.

 

I would suggest making a function for that only loop and using a return...

How you pass the needed variables to the function it's up to you (or whatever the program needs)

 
Manuel Alejandro Cercos Perez #:

I would suggest making a function for that only loop and using a return...

How you pass the needed variables to the function it's up to you (or whatever the program needs)

Thanks Manuel

This is creative suggestion, I will consider it.

Reason: