Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 20

 
Renat Akhtyamov:
for(int i=1; int i<int n; int i++)                                                                  
...
right??? Please.

No, I meant:

for( int i = 0; i < x; i++ )
{
}
for( int i = 0; i < x; i++ )
{
}
...

Yes, and the design you wrote won't work at all.

 
Artyom Trishkin:
Every variable has its own scope. Even inside curly braces, there is a separate scope for the variable declared in that scope - inside the curly braces. So why should I, for example, create loop index variables with different names in different places of a program, if I know that they do not overlap, and it is sufficient, and customary, to use such a variable name as "i"?

I did not argue with you.

 
Renat Akhtyamov:
for(int i=1; int i<int n; int i++)                                                                  
...
right??? You're welcome.
You yourself realise that you have deliberately made up a bad example that no sane programmer would make up.
 
Alexey Kozitsyn:

No, I meant:

for( int i = 0; i < x; i++ )
{
}
for( int i = 0; i < x; i++ )
{
}
...


what is the point? Please share your experience - what might be the problem in this case, i.e. if your code were like this?

int i;
for( i = 0; i < x; i++ )
{
}
for( i = 0; i < x; i++ )
{
}
...
 
Renat Akhtyamov:

I was not arguing with you.

This is my thread. And where I give bad, sub-optimal advice, I am entitled to correct the person. You, as far as the situation at hand is concerned, are giving bad advice.
 
Artyom Trishkin:
This is my thread. And where bad sub-optimal advice is given, I am entitled to correct the person. You, with respect to the situation at hand, are giving bad advice.

Your thread, then read it carefully and relate the post to the nickname you are replying to.

In short, you're in the wrong place.

 
Renat Akhtyamov:

but what is the point? Please share your experience - what could be the problem in this case, i.e. if your code would be like this?

int i;
for( i = 0; i < x; i++ )
{
}
for( i = 0; i < x; i++ )
{
}
...

When you initialize a counter outside a loop, its scope extends beyond the loop, which is usually not needed. Thus, you provide a fertile ground for possible errors, for example, if further in the code you will have a variable j, and you accidentally call it i. Or instead of index [1] you write [i], etc.

So, variables must be initialized as close as possible to where they are first used and avoid a large number of global variables. This reduces the list of possible errors.

 
Alexey Kozitsyn:

When you initialize a counter outside a loop, its scope extends beyond the loop, which is usually not necessary. By doing this you provide a fertile ground for possible errors, e.g. if further down the code you have a variable j and you accidentally call it i. Or instead of index [1] you write [i], etc.

So, variables must be initialized as close as possible to where they are first used and try to avoid a large number of global variables. This reduces the list of possible errors.

What does this have to do with(....)?

I'm outraged.

If it says i=0 and i++?

Well, change i in another loop?

It will not work

 
Renat Akhtyamov:

What's that got to do with it?

I'm out.

You can be anywhere and in anything, that's your right. You asked me to explain - I did.
 
Alexey Kozitsyn:
You can be anywhere and in anything, that's your right. You asked me to explain - I did.

The only thing to consider here is that after the loop block i already equals x, provided that in the loop i<x

Array[i] after for(...){} will return the array overrun error and that's it