Exiting multiple loops

Back to topics list To post a new topic, please log in or register
avatar
2
sydneylast 2016.06.08 16:38
 

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

                    }

            }

     }



avatar
1218
FMIC 2016.06.08 16:44 #
 

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.

avatar
2017
gooly 2016.06.08 19:46 #
 

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
} 


avatar
2
sydneylast 2016.06.09 14:55 #
 

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
avatar
2017
gooly 2016.06.10 10:46 #
 

Well, read about datetime.

Datetime is a number of seconds since ..

So you just would need:

datetime d = Time[0]+ 12*3600; // +12 Hours;


avatar
13006
WHRoeder 2016.06.10 13:57 #
 
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;

avatar
13006
WHRoeder 2016.06.10 14:10 #
 
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;
    }

avatar
1218
FMIC 2016.06.10 14:33 #
 
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).

Back to topics list  

To add comments, please log in or register