i find a if-else bug,please tell me why?

 
my source:
if(condition1)
  {
   operator1;
  }
else 
   if(condition2)
     {
      operator2;
     }
else 
   if(condition3)
     {
      operator3;
     }
//but metaeditor "catl+," format this:

if(condition1)
  {
   operator1;
  }
else 
   if(condition2)
     {
      operator2;
     }
   else 
     if(condition3)
      {
       operator3;
      }

my condition1,2,3 are same level,but metaeditor recognized as condition1,(2,3 are same level )are same level.

Please tell me how to solve this problem?

   if(PositionsTotal()>0)
     {
      // deal pos
     }
   else
      if(OrdersTotal()>0)
        {
         //deal order
        }
      else
        {
         // deal buy or sell
        }
 
c327515:

my condition1,2,3 are same level,but metaeditor recognized as condition1,(2,3 are same level )are same level.

Please tell me how to solve this problem?

I wrote about this in a topic that terminal developers read

 
The issue is due to the way the compiler interprets the nesting of your "if" statements when you structure them like this. In your code, using

if(PositionsTotal()>0)
{
   // deal pos
}
else
   if(OrdersTotal()>0)
   {
      // deal order
   }
   else
   {
      // deal buy or sell
   }

the compiler sees the second "if" as nested within the "else" of the first condition. If your intention is to have all three conditions on the same level, the preferred approach is to use the "else if" keyword combination. That way, it's clear that the second condition is tied to the first "if", and the third condition is an "else" case.

Try this version instead:

if(PositionsTotal() > 0)
{
   // deal with position
}
else if(OrdersTotal() > 0)
{
   // deal with order
}
else
{
   // deal buy or sell
}
 
Sivakumar Paul Suyambu #:
The issue is due to the way the compiler interprets the nesting of your "if" statements when you structure them like this. In your code, using

the compiler sees the second "if" as nested within the "else" of the first condition. If your intention is to have all three conditions on the same level, the preferred approach is to use the "else if" keyword combination. That way, it's clear that the second condition is tied to the first "if", and the third condition is an "else" case.

Try this version instead:

please see 1# reply video, or try to write this code in MetaEditor.
 
c327515 #:
please see 1# reply video, or try to write this code in MetaEditor.

It should be "else if" in 1 row, not 2 rows. Otherwhile, it think that you are using "if else", not "if and then else if"

 
Nguyen Tuan Son #:

It should be "else if" in 1 row, not 2 rows. Otherwhile, it think that you are using "if else", not "if and then else if"

no ,i write in 1 rows, but the MetaEditor format 2 rows,see 1# reply video or try do it
 
c327515 #:
no ,i write in 1 rows, but the MetaEditor format 2 rows,see 1# reply video or try do it

You're right. I misunderstood you. I tried and the same problem happens if I pick code style is MetaQuotes.

Fortunately, I don't prefer MetaQuotes code style and usually using Google style. This problem doesn't occur in Google code style (and some others as I tried). You can consider to use this option as a workaround.