I'm so new. Please help me understand this part...

 
I was under the impression that I could create a variable from inside a for loop and then use that variable outside the loop. That can't be done, can it?

I'm simply trying check every candle for certain conditions to be met and create a variable (or store that candle number or time) for use outside the loop. 

Please trust I AM trying. I'm searching the forums and documents. I'm really just not sure where to look. The course that is the foundation of my knowledge was limited. 

Your input is deeply appreciated. Thanks in advance. 
 
dasilvja:
I was under the impression that I could create a variable from inside a for loop and then use that variable outside the loop. That can't be done, can it?

I'm simply trying check every candle for certain conditions to be met and create a variable (or store that candle number or time) for use outside the loop. 

Please trust I AM trying. I'm searching the forums and documents. I'm really just not sure where to look. The course that is the foundation of my knowledge was limited. 

Your input is deeply appreciated. Thanks in advance. 

No it cannot be done, if you create the variable inside the loop then it is only available to the loop.

If you create it outside the loop but inside the function that contains the loop then it will be available in throughout the function including the loop

and if you create it in the global space then it would be available everywhere.

//Here iVariable is available throughout the OnStart() function including the loop;

void OnStart()
{
   int iVariable = 0;

   for(int iC=0; iC<10; iC++)
   {
     iVariable = iC;
   }

   Print("iVariable = " + iVariable);   
}


// here it will fail to compile because the Print statement has no access to iVariable
void OnStart()
{

   for(int iC=0; iC<10; iC++)
   {
     int iVariable = iC;
   }

   Print("iVariable = " + iVariable);  
}



// here you get two iVariables one for the loop one for the OnStart()  
// the loop will use the locally declared iVariable 
// the Print statement will print 99 as it accesses the first iVariable
 
void OnStart()
{
   int iVariable = 99;

   for(int iC=0; iC<10; iC++)
   {
     int iVariable = iC;
   }

   Print("iVariable = " + iVariable);  
}


Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
dasilvja:
I was under the impression that I could create a variable from inside a for loop and then use that variable outside the loop. That can't be done, can it?

I'm simply trying check every candle for certain conditions to be met and create a variable (or store that candle number or time) for use outside the loop. 

Please trust I AM trying. I'm searching the forums and documents. I'm really just not sure where to look. The course that is the foundation of my knowledge was limited. 

Your input is deeply appreciated. Thanks in advance. 


Here is the documentation on variable visibility: Visibility Scope and Lifetime of Variables

And that is also a good search phrase for Google so you can learn more about variable scope and lifetime.

Visibility Scope and Lifetime of Variables - Variables - Language Basics - MQL4 Reference
Visibility Scope and Lifetime of Variables - Variables - Language Basics - MQL4 Reference
  • docs.mql4.com
A variable declared outside all functions is located into the global scope. Access to such variables can be done from anywhere in the program.These variables are located in the global pool of memory, so their lifetime coincides with the lifetime of the program. A variable declared inside a block (part of code enclosed in curly brackets) belongs...
 
This is exactly what I've been looking for! Thank you so much, both of you. I'll read up on it now. I could cry I'm so happy. lol So frustrating not understanding this. 
Reason: