How does static variables 'behaves' inside a loop

 

Hey guys,


I am away from my personal computer for a few days, so I can't test it myself, and I couldn't find an answer online,

But suddenly it occurred to me that perhaps I have a logical bug in a code I've written.


My question is-

How does a static variable 'behaves' inside a loop?


Consider the following example (this piece of code is just to illustrate my question)-


void OnTick() {
   for(int i = 0; i < 7; i++) {

      static int iCount = 0;
      iCount += i;
   }
}


Would the static iCount variable will act as sort-of a global variable, which means that all iterations of the loop will access the same instance/value,

Or does each iteration will have its own individual instance of iCount?


I am not an native English speaker,

So I hope I've phrased my question clear enough.


Any input will be appreciated guys.

Thanks.

 

It behaves as global variable, except that it can be accessed only inside this loop. but 0 will be assigned to it only once.

https://www.mql5.com/en/docs/basis/variables/static

Documentation on MQL5: Language Basics / Variables / Static Variables
Documentation on MQL5: Language Basics / Variables / Static Variables
  • www.mql5.com
Static Variables - Variables - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: