Questions from Beginners MQL5 MT5 MetaTrader 5 - page 497

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Here it says in whitehttps://docs.mql4.com/ru/basis/operators/for
https://docs.mql4.com/ru/basis/variables/variable_scope
Thanks, but I've read the first link before and the second one confirms the logic I used when writing the code.
However, I'm confused by this statement in the second link "... There isanother variable with the same name outside this loop... "It follows that the value of a global variable remains the same despite the fact that inside the block a new variable with the same name will be declared, but this nonsense because if you declare a new variable, the value of the former will be erased, then I do not understand, what is the point of using and storing two different variables (local and global), but with the same value?
Thanks, but I've read the first link before, and the second confirmed the logic I used when writing the code.
However, I'm confused by this statement from the second link "...outside this loop there is another variable with the same name... " But this is nonsense because if you declare a new variable, the value of the former will be erased, then I do not understand, what's the point of using and storing two different variables (local and global), but with the same value?
In my opinion, you are writing nonsense. If you declare a local variable inside a loop or function, the value of a global variable with the same name won't be affected, but the compiler will warn you that you declared a local variable with the same name as the existing global variable and working inside a function or code block whose scope includes the newly declared variable will not affect the value of the global variable. It warns you about this so that you understand that this is a new variable, and so that you don't expect the value of the global variable to change after you leave the scope of the block in which you declared a new variable with the same name as the existing global variable.
To put it simply: everything inside the flat applies to all rooms and is shared by all rooms (global variable). And everything that is inside a room - it only applies to the room and not visible from other rooms (it is a local variable).
Even simpler? Please:
If you mess up the air in the toilet (locally), that's where it's messed up. Meanwhile, the whole flat (globally) knows (as a result of your toilet experiments) that it's better not to go into the toilet... But the air is a concept for the whole flat, and all residents know about the spoiled air in the toilet. At the same time, only the lucky person who is in the toilet can fully experience all the delights of changing the local environment within the confined space (local influence of the value of the variable). The rest of us know about it, but don't feel it. When you leave the toilet, you lose the sensations (local values) that you experienced in the toilet and begin to breathe fresh air (get the global values of air), while knowing, like everyone else, that there is a kick in the pants (the result of the air contamination function, which affects the overall life of the entire flat)...
I hope I've made myself clear
I think you are writing nonsense. If you declare a local variable inside a loop or function, the value of the global variable with the same name will not be overwritten, but the compiler will warn you that you declared a local variable with the same name as the existing global variable, and working inside a function or code block whose scope includes the newly declared variable will not affect the value of the global variable. It warns you about this so that you understand that this is a new variable, and so that you don't expect the value of the global variable to change after you leave the scope of the block in which you declared a new variable with the same name as the existing global variable.
In simple terms: everything inside the flat applies to all rooms and is shared by all rooms (global variable). And everything that is inside a room - it only applies to the room and not visible from other rooms (it is a local variable).
Even simpler? Please:
If you mess up the air in the toilet (locally), that's where it's messed up. Meanwhile, the whole flat (globally) knows (the result of your toilet experiments) that it's better not to go into the toilet... But the air is a concept for the whole flat, and all the occupants know about the spoiled air in the toilet. At the same time, only the lucky person who is in the toilet can fully experience all the delights of changing the local environment within the confined space (local influence of the value of the variable). The rest of us know about it, but don't feel it. When you leave the toilet, you lose the sensations (local values) that you experienced in the toilet and begin to breathe fresh air (get the global values of air), while knowing, like everyone else, that there is a kick in the pants (the result of the air contamination function, which affects the overall life of the entire flat)...
I hope I've made myself clear
Discovered the truth in your opus - thank you! It turns out that declaring a variable inside a block with the same name as the global variable results in blocking data from the global variable inside the block... and the warning about the already declared variable cannot be removed, right?
Thanks, but I've read the first link before, and the second confirmed the logic I used when writing the code.
However, I'm confused by this statement from the second link "...outside this loop there is another variable with the same name... "But this nonsense because if you declare a new variable, the value of the former will be erased, then it is not clear to me, what's the point of using and storing two different variables (local and global), but with the same value?
Unfortunately it's not written correctly :(
Unfortunately, it's misspelled there :(
it doesn't say it right in the help.
Why are you giving me the code? I don't know what to look for in it... ...and I'm limited by time. Can you tell me what it's for and what the error is in the help.
is the code from the help
is "i" at the beginning of the function
then in the loop it's "i" again.
is the code from the help
is "i" at the beginning of the function
then in the loop it is "i" again
Well there is no error: a loop with global i (blue), and another loop with local i (red)