[ANSWERED]Declaring inside blocks | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[ANSWERED]Declaring inside blocks

https://www.sololearn.com/Discuss/320499/?ref=app I'm longing for answers... To paraphrase, how does the block (loop, for example) communicates with the outer code? Why declaring a variable inside a loop doesnt take it outside, but declaring it outside, passing it to the loop makes the result accessible? Is int i=0 declared inside the loop or outside? Seems that outside. Thats the moment where my brains begin to boil. Please help to cool it down. Some study material on this topic would be great for me.

17th Apr 2017, 7:00 PM
Alex Snaidars
Alex Snaidars - avatar
1 Answer
+ 3
A block is a variable scope. That is why a variable declared in a block isn't visible outside because when the block ends the scope and its variables are removed. A scope can see parent scopes that is why a variable declared outside the scope is visible in the block. It is useful to handle variable scopes and free memory as soon as possible.
17th Apr 2017, 7:18 PM
Tamás Barta
Tamás Barta - avatar