Question of declaration

 

Hello,



Can we declare variables in a loop, by this way for exemple


for(i=1,i=10,i++)

{

variable+"i"= value+"i";

}


Thank you for your help

 
crystal7:

Hello,

Can we declare variables in a loop, by this way for exemple


Nope, consider using an array instead.
 

Thank you,



What is the faster for a programm?

To declare variables one by one, or in an array?

 
crystal7:

Thank you,

What is the faster for a programm?

To declare variables one by one, or in an array?

this is a bit like splitting hairs. either method works fine and the difference in speed between the two is going to be hardly noticable.

if you are having problems needing to optimize metatrader code, omg, get a faster computer, i run my metatrader on a p2 processor and my EA runs like greased butter.

/*
question: Can we declare variables in a loop, by this way for exemple?

answer: a correct syntax might look like this:


int i;

for(i=1,i<10,i++)
{
i=i+1; 

//or you could use the shorthand version of: 
i++;
//i++ is shorthand for i=i+1;

//
//your posted syntax is entirely incorrect.
//variable+"i"= value+"i";
}

 
silverstar4x:

this is a bit like splitting hairs. either method works fine and the difference in speed between the two is going to be hardly noticable.

if you are having problems needing to optimize metatrader code, omg, get a faster computer, i run my metatrader on a p2 processor and my EA runs like greased butter.


But I tried my syntax and it doesn't works,I think the array is the better solution, but I want to put string variables in it.

Is this possible?

I don notr succeed.

 
crystal7:

But I tried my syntax and it doesn't works,I think the array is the better solution, but I want to put string variables in it.

Is this possible?

I don notr succeed.


//--------------------------------------------------------------------
// stringarray.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
extern double Level=1.3200;                     // Preset level 
string Text[101];                               // Array declaration
//--------------------------------------------------------------------
int init()                                      // Special funct. init()
  {                                             // Assigning values
   Text[1]="one ";             Text[15]="fifteen ";
   Text[2]="two ";             Text[16]="sixteen ";
   Text[3]="three ";           Text[17]="seventeen ";
   Text[4]="four ";            Text[18]="eighteen ";
   Text[5]="five ";            Text[19]="nineteen ";
   Text[6]="six ";             Text[20]="twenty ";
   Text[7]="seven ";           Text[30]="thirty ";
   Text[8]="eight ";           Text[40]="forty ";
   Text[9]="nine ";            Text[50]="fifty ";
   Text[10]="ten ";            Text[60]="sixty";
   Text[11]="eleven ";         Text[70]="seventy ";
   Text[12]="twelve ";         Text[80]="eighty ";
   Text[13]="thirteen ";       Text[90]="ninety";
   Text[14]="fourteen ";       Text[100]= "hundred";
   // Calculating values
   for(int i=20; i<=90; i=i+10)                // Cycle for tens
     {
      for(int j=1; j<=9; j++)                  // Cycle for units
         Text[i+j]=Text[i] + Text[j];          // Calculating value   
     }
   return;                                     // Exit init()
  }
//--------------------------------------------------------------------
int start()                                     // Special funct. start()
  {
   int Delta=NormalizeDouble((Bid-Level)/Point,0);// Excess 
//--------------------------------------------------------------------
   if (Delta>=0)                                // Price is not higher than level
     {
      Alert("Price below level");               // Alert
      return;                                   // Exit start()
     }
//--------------------------------------------------------------------
   if (Delta<100)                               // Price higher than 100
     {
      Alert("More than hundred points");        // Alert
      return;                                   // Exit start()
     }
//--------------------------------------------------------------------
   Alert("Plus ",Text[Delta],"pt.");            // Displaying
   return;                                      // Exit start()
  }
//--------------------------------------------------------------------
Yes it is .. https://book.mql4.com/variables/arrays
Reason: