C
C# Learner
I've been told on here that the following is equivalent:
<snippet 1>
int a;
for (int i = 0; i < 10; ++i) {
a = GetValueFromSomewhere();
}
</snippet 1>
<snippet 2>
for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}
</snippet 2>
In the second snippet, I cannot use 'a' outside of the loop. Why?
For example, the following causes a compile error ("The name 'a' does
not exist in the class or namespace"):
<snippet 3>
for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}
a = AnotherValue();
</snippet 3>
<snippet 1>
int a;
for (int i = 0; i < 10; ++i) {
a = GetValueFromSomewhere();
}
</snippet 1>
<snippet 2>
for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}
</snippet 2>
In the second snippet, I cannot use 'a' outside of the loop. Why?
For example, the following causes a compile error ("The name 'a' does
not exist in the class or namespace"):
<snippet 3>
for (int i = 0; i < 10; ++i) {
int a = GetValueFromSomewhere();
}
a = AnotherValue();
</snippet 3>