# Exiting multiple loops

12

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])

}

}

}

```
2956

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.

7362

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(false); // nothing found
}
```
12

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
7362

Datetime is a number of seconds since ..

So you just would need:

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

 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;`
16099

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;
}```

2956

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.