Download MetaTrader 5

Exiting multiple loops

To add comments, please log in or register
sydneylast
12
sydneylast  

Hi!

I have 3 nested loops and upon a condition in the innermost loop I'd like to exit the inner two loops and continue the outer loop. Is there is an easy way to do this?


//+----------------------------------------------------------------------+

int Swinghighs[50];

int Swinglows[50];

int x, y, z;


for(x=50;x>=0;x--)

   { for(y=50;y>=0;y--)

           {for(z=50;z>=0;z--)

                {

                    if(High[z]>High[x])

                           break;    //<======= I want to return to the x loop

                    }

            }

     }


Fernando Carreiro
2320
Fernando Carreiro  

Please note that this code sample was not compiled or tested (it serves only to exemplify a possible solution):

int Swinghighs[50];
int Swinglows[50];
int x, y, z;

for( x = 50; x >= 0; x-- )
{
   bool boolBreakX = false;

   for( y = 50; y >= 0; y--)
   {
      for( z = 50; z >= 0; z-- )
      {
         if( High[ z ] > High[ x ] )
         {
            boolBreakX = true;
            break;
         }
      }
      
      if( boolBreakX ) break;
   }
}

Also, please EDIT your post and put your code into a SRC block as I did. Just use the "SRC" icon on the posting toolbar.

Carl Schreiber
7226
Carl Schreiber  

what about putting everything in a function and leave it?

bool multLoop ( int &x, int &y, int &z) {
for(x=50;x>=0;x--)
   { for(y=50;y>=0;y--)
           {for(z=50;z>=0;z--)
                {
                    if(High[z]>High[x])
                           return(true);    // found<======= I want to return to the x loop
                 }
            }
     }
 return(false); // nothing found
} 
sydneylast
12
sydneylast  

Thank you all for your replies.


I have a different problem and the system won't let me post a new topic ( probably because I'm new) so I'll post it here.


I would like to draw a triangle in which the leftmost point extends beyond Bar 0.  What is the proper method for

datetime d = Time[0]+ 12 Hours;

?


TIA
Carl Schreiber
7226
Carl Schreiber  

Well, read about datetime.

Datetime is a number of seconds since ..

So you just would need:

datetime d = Time[0]+ 12*3600; // +12 Hours;
whroeder1
15712
whroeder1  
gooly: So you just would need:
datetime d = Time[0]+ 12*3600; // +12 Hours;
Or self-documenting.
datetime d = Time[0]+ 12*PeriodSeconds(PERIOD_H1); // +12 Hours;
whroeder1
15712
whroeder1  
sydneylast: I have 3 nested loops and upon a condition in the innermost loop I'd like to exit the inner two loops and continue the outer loop. Is there is an easy way to do this?
  1. If there is nothing after the z loop, no need for extra variables FMIC. Loop variables can be modified.
    for(x=50;x>=0;x--){ 
       for(y=50;y>=0;y--){
          for(z=50;z>=0;z--){
             if(High[z]>High[x]){
                y = 0;    // exit y
                break;    // exit z
             }
             // more z
          } // z
       }  // y
    }  // z
    If there are, then test after z loop is required. Either if(bool boolBreakX) or if(y < 0) break.
  2. I agree with gooly. It is probably best to factor out a function.
    for(x=50;x>=0;x--){ 
       if( function(x) ) ...
    }  // z
    ///
    bool function(int shift){
       double p =  High[shift];
       for(y=50;y>=0;y--){
          for(z=50;z>=0;z--){
             if(High[z] > price){
                return false;
             }
             // more z
          } // z
          // more y
       }  // y
      return true;
    }

Fernando Carreiro
2320
Fernando Carreiro  
WHRoeder:
  1. If there is nothing after the z loop, no need for extra variables FMIC. Loop variables can be modified.If there are, then test after z loop is required. Either if(bool boolBreakX) or if(y < 0) break.
  2. I agree with gooly. It is probably best to factor out a function.

The reason I still believe the "boolBreakX" to be more robust, is that even if the loop parameters changes during code development, one does not have to keep track of those conditions, as is your case, where you alter the loop value in order to exit the loop. With the "boolBreakX", even if one changes the loop variable name or its limit values, the code still executes and does not need to be adjusted.

However, "all roads lead to Rome", so all three solutions are valid (each with its own advantages and disadvantages).

To add comments, please log in or register