[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 721

 
Abzasc:

The point is not to multiply variables, many conditions and counters.




the point is to write correctly then, not to use language abbreviations:

int a;
for(a=0;a<8;a++){
//здесь что то делаем
}
for(a=0;a<8;a++){
//делаем что нибудь другое
}
 
Abzasc:

The point is not to create variables, lots of conditions and counters.

So please, the compiler allows you to do such things...

//+------------------------------------------------------------------+
int start()
  {
  bool flag;
  
   if(flag){
      for(int a = 0; a < 10;a++){
         Print("a = ", a);
      }
   }else{
      for(a = 10; a > 0;a--){
         Print("a = ", a);
      }
   }
  }
//+------------------------------------------------------------------+
The default flag is False, so the first for loop in which the variable a is declared will not be executed, which does not prevent the same "undeclared" variable from being used in the else branch : )
 
Abzasc:

The condition if

here we do something

otherwise if another condition

do something else

The point is not to create variables, lots of conditions and counters.

This does not require you to re-initialise the variable. Do everything according to what you have written and your variable a will be used in the right places in your logical branches without re-initialisation. Declare it once and use it for your benefit and the benefit of mankind... :) The main thing is not to get lost where and what you use it for...
 
ToLik_SRGV:

so the first for loop in which the variable a is declared will not be executed

The loop must always be executed.
 
IgorM:


the point of writing correctly then, rather than using abbreviations of language:

The whole problem was the lack of {}, wasn't it? Thank you!
 
artmedia70:
This does not require you to re-initialise the variable. Do everything according to what you have written and your variable a will be used in the right places in your logical branches without re-initialisation. Declare it once and use it for your benefit and the benefit of mankind... :) The main thing is not to get lost where and what you use it for...
I did so, on 715 pp, 18.07.2010 14:56, didn't put brackets after counter...
 

The "effect" shown above proves that MQL-machine first looks through EX4 file for declared variables, and creates them regardless of logical branches in full. On the one hand it turns out not to save RAM, and on the other hand you can create similar constructions, although in my opinion this style is not the best option.

 
Abzasc:
The whole problem was in the lack of {}, wasn't it? Thanks a lot!


The whole problem was in the declaration of the variable:

int a;

I declare it and then use it where and when needed, while you had a repeated declaration of a variable - most likely the compiler didn't skip it

If you have only one statement in the loop then you don't need {}, but if you have more then you have to put {}, otherwise you will look for why the loop isn't calculated correctly

SZZY: If you have just started, put curly brackets after for, you will get the hang of it later

 
ToLik_SRGV:

IgorM's code does not give any errors. I don't think a will be allocated memory depending on the number of mentions in the branches, so it's OK.
 
Abzasc:
The loop must always be executed.

What does the loop have to do with it? It's just an example that a variable created in a block which is not used is still in "play" and will be visible and usable throughout the start() method, without the need to re-initialize it, much less de-initialize it before reuse.

Reason: