Multiple if statement, else code is not executed - page 2

 

another test if it helps :

  bool IO1=false;
  bool IO2=false;

  string collect="";
  if(IO1)
    Print("IO1 true");
    if(!IO2)
    {
    collect+="A";
    }
    if(!IO2&&!IO1)
    {
    collect+="B";
    }
  Print("Collected ["+collect+"]");

See what happens when you remove the "Print("IO1 true");" line 

You need to link if statements or have a loop following a non bracketed if , you get one "next line" of code to link what goes in the if block .

So in the example above as is the 1st Print is taking up that line and then what follows belongs to the function scope not the if(IO1)

Similarly , if you remove the 1st print then the if(!IO2) belongs to the if(IO1) but the if(!IO2&&!IO1) belongs to the function scope, unless it was an else if (linking to the previous which was part of the if(IO1) block.

 
Lorentzos Roussos #:

another test if it helps :

See what happens when you remove the "Print("IO1 true");" line 

You need to link if statements or have a loop following a non bracketed if , you get one "next line" of code to link what goes in the if block .

So in the example above as is the 1st Print is taking up that line and then what follows belongs to the function scope not the if(IO1)

Similarly , if you remove the 1st print then the if(!IO2) belongs to the if(IO1) but the if(!IO2&&!IO1) belongs to the function scope, unless it was an else if (linking to the previous which was part of the if(IO1) block.

Yes you are right … if you remove that line, then second if will be executed only if first if is true … good to know … normally I use brackets as is styling better, but good to know.  Thanks 
 
ahmedelmidany #: The part having this comment 
USE_INDICATOR-X_FOR_ENTRY = false;

               if(USE_INDICATOR-X_FOR_ENTRY)  //if a specific indicator "the flag USE_INDICATOR-X_FOR_ENTRY" is used to confirm entry in position

Do not post code that will not even compile. "A - B = false;" is nonsense.

 

Hi

I recommend using braces - especially with multiple-lined ifs’. I think here the “else” part is not properly read.

For example, here –this second else is connected to “signal type” condition or “indicator X for entry” condition? Add proper braces and it should work then.

if(gobuy == 1) //if a Buy signal was found
         {
            openPrice = Open[0];
            indicatorX_down = iCustom(_Symbol,PERIOD_CURRENT,"indicator-x",2,0);
            Print("open price: ", openPrice, " - Indicator-X down: ", indicatorX_down);
            if(SIGNAL_TYPE == 0 || SIGNAL_TYPE == 2){ //means BUY signals are allowed by the variable SIGNAL_TYPE
               if(USE_INDICATOR-X_FOR_ENTRY) { //if a specific indicator "the flag USE_INDICATOR-X_FOR_ENTRY" is used to confirm entry in position
                  if(openPrice < indicatorX_down)
                  {
                     datetime time = iTime(NULL,0,1);
                     Print("Indicator-X Buy Entry condition met");
                     if(PushAlert)SendNotification(Symbol()+" BUY @  Hour "+string(Hour())+"  Minute "+string(Minute()));
                     if(EmailOn)SendMail("PC BUY SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Buy - "+ChartSymbol(0)+"\r\n");
                     orderCount +=1;
                     inBuy=true;
                     Print("In buy: ", IntegerToString(inBuy));
                  }
                  else{
                     Print("Indicator-X Buy Entry condition not met");
                  }
               }
                else
               {
                  Print("Indicator-x condition not used");  //////THIS PART OF CODE IS NOT EXECUTED ALTHOUGH FLAG USE_INDICATOR-X_FOR ENTRY IS FALSE///////
                  datetime time = iTime(NULL,0,1);
                  if(PushAlert)SendNotification(Symbol()+" BUY @  Hour "+string(Hour())+"  Minute "+string(Minute()));
                  if(EmailOn)SendMail("PC BUY SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Buy - "+ChartSymbol(0)+"\r\n");
                  orderCount +=1;
                  inBuy=true;
                  Print("In buy: ", IntegerToString(inBuy));
               }
           }
        }




 
Daniel Cioca #:
Yes you are right … if you remove that line, then second if will be executed only if first if is true … good to know … normally I use brackets as is styling better, but good to know.  Thanks 
That’s clear now, thanks 
 
ahmedelmidany #:

Thanks Fernando, this is actually what i was doing but it was not working....i will check yours and see if all will be good.

Thanks again Fernando, it works now
I just added the closing braces to your code for the gobuy/gosell if statements 
 
Marzena Maria Szmit #:

Hi

I recommend using braces - especially with multiple-lined ifs’. I think here the “else” part is not properly read.

For example, here –this second else is connected to “signal type” condition or “indicator X for entry” condition? Add proper braces and it should work then.




Yeah it worked after adjusting the braces
 
Lorentzos Roussos #:

another test if it helps :

See what happens when you remove the "Print("IO1 true");" line 

You need to link if statements or have a loop following a non bracketed if , you get one "next line" of code to link what goes in the if block .

So in the example above as is the 1st Print is taking up that line and then what follows belongs to the function scope not the if(IO1)

Similarly , if you remove the 1st print then the if(!IO2) belongs to the if(IO1) but the if(!IO2&&!IO1) belongs to the function scope, unless it was an else if (linking to the previous which was part of the if(IO1) block.

It is clear now, thanks to your explanation
[Deleted]  
ahmedelmidany #: Thanks again Fernando, it works now
I just added the closing braces to your code for the gobuy/gosell if statements 
You are welcome!