About cycle control, help!

 

I wrote the following to close orders. Actually I set two different situations to close long positions. The structure is as follows:

for (int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if (OrderType() == OP_BUY) 

   if(*******) 
    {
     if(*****) return;
     if(*****) return;
     closebuy();
    }

   if(*******) 
    {
     if(*****) return;
     if(*****) return;
     closebuy();
    }
}

The question is that when the first case ends with "return", it seems won't excute the second case checking and directly jump out of the whole cyle. What I want is to treat the two cases equally and if the first case breaks with "return", the system goes on to check the second case. I am not sure if I made myself clear...

Thanks!

 

No, not clear yet.

Return is an immediate exit, rethink your logic.

Maybe use:

if( ***** || *****) return;

closebuy();

---

or

---

if(!***** && !******) closebuy();

 
   if(*******) 
    {
     if(*****) return;
     if(*****) return;
     closebuy();
    }
I see the above as a small cycle. Actually you can see that there are two same-type cycles to check if the positions should be closed. I'd like the "return" in the first cycle to exit just the first small cycle, not the whole buy-close checking function.
 
Did I use "return" in a wrong way? What I want is to let the system check the second small cycle when the first one exit with "return" (not with closebuy).
 

return means to abandon processing... you are finished with some task, or want to abort...

what gets abandoned depends on the context of the position of the return in the code.

return in a function returns you to the line after the function call

return not in a function exits the program, until it is restarted by the next tick.

https://docs.mql4.com/basis/operators/return

---

Maybe you want to use "continue" which puts you into the next iteration of the for loop.

---

Maybe you want to use "break" which exits the for loop.

 
Yeah, I think I would do some research on "continue" and "break". They might be the solutions. Thanks phy!
Reason: