Odd behaviour of WHILE operator

 

Hello all!

I've an EA (never tested it on pre 600 builds but the code is 2 years old) that gets stuck during a WHILE on build 625.

The fact is that when the WHILE starts it seems to be unable to check the expressions: the result is that it never comes out . It is placed in the start function.

I've tried a very simple EA in order to understand what goes on:


int counter=0, MaxCount = 10000; 

void start()
  {
  while( counter <= MaxCount )
   {Print("Counter ", counter);
   counter++;
   }
   return;
  }

Well, the first counter value usually starts from a random value above 9500 and not from 0.

Why is it? Any suggestion?

 

maybe the counter gets another value somewhere else inside the EA try :

void start()
  {
  int counter=0, MaxCount = 10000;
  while( counter <= MaxCount )
   {Print("Counter ", counter);
   counter++;
   }
   return;
  }
 
lord_hiro:

Hello all!

I've an EA (never tested it on pre 600 builds but the code is 2 years old) that gets stuck during a WHILE on build 625.

The fact is that when the WHILE starts it seems to be unable to check the expressions: the result is that it never comes out . It is placed in the start function.

I've tried a very simple EA in order to understand what goes on:

Well, the first counter value usually starts from a random value above 9500 and not from 0.

Why is it? Any suggestion?

You declared the counter globaly that means it is static.

Your code in OnStart(), counter++ pushes the counter value all the way up to 10,000

So then because it is static, at the next tick your counter is at 10,000 to begin with.

 
SDC:

You declared the counter globaly that means it is static.

Your code in OnStart(), counter++ pushes the counter value all the way up to 10,000

So then because it is static, at the next tick your counter is at 10,000 to begin with.


he wrote:

lord_hiro:

Well, the first counter value usually starts from a random value above 9500 and not from 0.

Why is it? Any suggestion?


so, this is my answer


qjol:

maybe the counter gets another value somewhere else inside the EA try :

void start()
  {
  int counter=0, MaxCount = 10000;
  while( counter <= MaxCount )
   {Print("Counter ", counter);
   counter++;
   }
   return;
  }
 
lord_hiro:

Hello all!

I've an EA (never tested it on pre 600 builds but the code is 2 years old) that gets stuck during a WHILE on build 625.

The fact is that when the WHILE starts it seems to be unable to check the expressions: the result is that it never comes out . It is placed in the start function.

I've tried a very simple EA in order to understand what goes on:


Well, the first counter value usually starts from a random value above 9500 and not from 0.

Why is it? Any suggestion?



Most likely you are looking in the Experts tab, which cannot keep up with fast loops like this.

Open the actual log file.

 

I can tell you one thing, that while loop would never execute a second time unless somewhere else in the code you call that counter variable again and change its value back to less than 10,001. You should be careful about declaring variables globably.

 
lord_hiro: Well, the first counter value usually starts from a random value above 9500 and not from 0. Why is it? Any suggestion?
Prior to build 600 uninitialized variables were initialized to zero. Now they contain random values, unless you initialize them..
 
He did initialize them, it must be something else changing the value of that counter variable...
 
As GumRai said, the log can't handle that speed, it skips most of the actual data and shows you only some fragments.
 

GumRai, you are right: the log reports all the outputs starting from 1.

The difference between putting the variable declaration in the global space instead of inside OnStart() is that in the first case the loop executes one time whilst in the second one it repeats indefinitely.

But... It's evident that I choose the wrong debugging example because the count cycle executes well.

It all started with the following EA.

Here is the code:

 extern int SwingBarCount = 100;
int start()



{

int SwingHighShift = 0;
string StringHighStatus = "False";
int SwingHigh = 0;


while (StringHighStatus == "False" || SwingHighShift <= SwingBarCount)
   {
   
   if(iFractals(NULL, 0, MODE_UPPER, SwingHighShift) == iHigh(NULL, 0, SwingHighShift) && iFractals(NULL, 0, MODE_UPPER, SwingHighShift) > Close[0])
      {
      StringHighStatus = "True";
      SwingHigh = SwingHighShift;
      ObjectDelete("SwingHigh");
      ObjectCreate("SwingHigh", OBJ_VLINE, 0, Time[SwingHigh], 0);
      ObjectSet("SwingHigh", OBJPROP_COLOR, Red);
      }
      else
      {
      SwingHighShift++;
      }

   }

}}

I've removed the other non relevant code for clarity and it executes till the WHILE because I put some breakpoints before.

It should start with the WHILE condition being true and cycling the IF ELSE until StringHighStatus becomes true or StringHighShift reaches SwingBarCount.

What I see instead is that it never ends because after the WHILE there is a series of COMMENT and PRINT commands that return no output.

Whilst StringHighStatus can remain false, the counter has to reach SwingBarCount.

I had to modify like this in order for it to work :

while (!EndCycle)
   {
   
   if(iFractals(NULL, 0, MODE_UPPER, SwingHighShift) == iHigh(NULL, 0, SwingHighShift) && iFractals(NULL, 0, MODE_UPPER, SwingHighShift) > Close[0])
      {
      StringHighStatus = "True";
      SwingHigh = SwingHighShift;
      ObjectDelete("SwingHigh");
      ObjectCreate("SwingHigh", OBJ_VLINE, 0, Time[SwingHigh], 0);
      ObjectSet("SwingHigh", OBJPROP_COLOR, Red);
      }
      else
      {
      SwingHighShift++;
      }
      if( StringHighStatus == "True" ) EndCycle = TRUE;
      if( SwingHighShift > SwingBarCount ) EndCycle = TRUE;
   }

I don't understant why it processes EndCycle and not the other condition.

Thnk you all for your replies!

 
if( StringHighStatus == "True" ) EndCycle = TRUE;
else if( SwingHighShift > SwingBarCount ) EndCycle = TRUE;
try include else.