If condition within For loop

 

Hi,

Is there any restriction on using an "if condition" within a "For Loop" or even a "Do while loop"?

I have a code with a following structure:

for(expression1; expression2; expression3)
   {
   expression=True;
   
   if (expression)
     operator1;
    else
     operator2;
   }

Instead, on compilation, I get an error message that is not even on the relevant lines but way up in my code.

However, once I remove "if" and "else", my code works.

Thank you,

 

Mark Phohole: Is there any restriction on using an "if condition" within a "For Loop" or even a "Do while loop"? I have a code with a following structure:

for(expression1; expression2; expression3)
{
   expression=True;
   
   if (expression)
      operator1;
   else
      operator2;
} 

Instead, on compilation, I get an error message that is not even on the relevant lines but way up in my code. However, once I remove "if" and "else", my code works.

There is no restriction. There is something else wrong in your code. Show your real code if you require assistance.

And please use the "</>" icon or Alt-S to add code to your post, instead of just plain text.

 
Fernando Carreiro #:

There is no restriction. There is something else wrong in your code. Show your real code if you require assistance.

And please use the "</>" icon or Alt-S to add code to your post, instead of just plain text.

Hi Fernando,

Thank you for the guidance.

Here is the code:

for(Shift_Pos=27;(Shift_Pos<53 && CS_N_Crossed); Shift_Pos++) 
      {
      double CS_N1_Value=iIchimoku(symbolName,TimeFrameInUse,9,26,52,MODE_CHIKOUSPAN,Shift_Pos);
      double CS_N2_Value=iIchimoku(symbolName,TimeFrameInUse,9,26,52,MODE_CHIKOUSPAN,Shift_Pos+1);
      double CP_N1_Value=iClose(symbolName,TimeFrameInUse,Shift_Pos);
      double CP_N2_Value=iClose(symbolName,TimeFrameInUse,Shift_Pos+1);
            
      If((CS_N2_Value<CP_N2_Value) && (CS_N1_Value>CP_N1_Value))
         {
         CS_N_Value=iIchimoku(symbolName,TimeFrameInUse,9,26,52,MODE_CHIKOUSPAN,Shift_Pos);
         CS_N_Crossed=True;
         //break;
         }
         else CS_N_Value=iIchimoku(symbolName,TimeFrameInUse,9,26,52,MODE_CHIKOUSPAN,27);
         
      }

I'm trying to determine the bar when chikou span closes above the close price and I search from bar 26 and end at bar 52.

Thank you.

 
Mark Phohole #:

Hi Fernando,

Thank you for the guidance.

Here is the code:

I'm trying to determine the bar when chikou span closes above the close price and I search from bar 26 and end at bar 52.

Thank you.

for(Shift_Pos=27;(Shift_Pos<53 && CS_N_Crossed==False); Shift_Pos++) 
      {
      double CS_N1_Value=iIchimoku(symbolName,TimeFrameInUse,9,26,52,MODE_CHIKOUSPAN,Shift_Pos);
      double CS_N2_Value=iIchimoku(symbolName,TimeFrameInUse,9,26,52,MODE_CHIKOUSPAN,Shift_Pos+1);
      double CP_N1_Value=iClose(symbolName,TimeFrameInUse,Shift_Pos);
      double CP_N2_Value=iClose(symbolName,TimeFrameInUse,Shift_Pos+1);
            
      If((CS_N2_Value<CP_N2_Value) && (CS_N1_Value>CP_N1_Value))
         {
         CS_N_Value=iIchimoku(symbolName,TimeFrameInUse,9,26,52,MODE_CHIKOUSPAN,Shift_Pos);
         CS_N_Crossed=True;
         //break;
         }
         else CS_N_Value=iIchimoku(symbolName,TimeFrameInUse,9,26,52,MODE_CHIKOUSPAN,27);
         
      }
 
Mark Phohole #: I'm trying to determine the bar when chikou span closes above the close price and I search from bar 26 and end at bar 52.
Please explain in more detail what it is you are trying to achieve. Explain what you expect the code to do and what it is not doing for you.
 
      If((CS_N2_Value<CP_N2_Value) && (CS_N1_Value>CP_N1_Value))

Did you create a boolean function named “If?” Or did you mean the operator if?

Mark Phohole: Is there any restriction on using an "if condition" within a "For Loop"
Your code has no such thing.
 
Mark Phohole #:

I never tried having an embedded && in the for statement -personally I prefer simpler code so I can debug it more easily:

for(Shift_Pos=27;Shift_Pos<53 ; Shift_Pos++) 

      {

if (!CS_N_Crossed) continue;

....

}

Also being an old school guy from the days when memory and CPU were never enough, we were taught not to declare variables inside loops to avoid resource overhead and improve performance.

So although I appreciate it may be less important today with GB of RAM, GHz CPUs and smarter compilers, my inclination would still be to write it like this:

      double CS_N1_Value=0; 
      double CS_N2_Value=0; 
      double CP_N1_Value=0; 
      double CP_N2_Value=0;
            
for(Shift_Pos=27;Shift_Pos<53 ; Shift_Pos++) 
      {
        if (!CS_N_Crossed) continue;
        CS_N1_Value=iIchimoku(symbolName,TimeFrameInUse,9,26,52,MODE_CHIKOUSPAN,Shift_Pos);
        CS_N2_Value=iIchimoku(symbolName,TimeFrameInUse,9,26,52,MODE_CHIKOUSPAN,Shift_Pos+1);
        CP_N1_Value=iClose(symbolName,TimeFrameInUse,Shift_Pos);
        CP_N2_Value=iClose(symbolName,TimeFrameInUse,Shift_Pos+1);
        
    	if((CS_N2_Value<CP_N2_Value) && (CS_N1_Value>CP_N1_Value)) // not If
        ....

        }


If that does not help, debug...