i
inside the
for
:
// Declare loop control variable inside the for.
using System;
class ForVar {
static void Main() {
int sum = 0;
int fact = 1;
// Compute the factorial of the numbers 1 through 5.
for(int i = 1; i <= 5; i++) {
sum += i; // i is known throughout the loop.
fact *= i;
}
// But, i is not known here.
Console.WriteLine("Sum is " + sum);
Console.WriteLine("Factorial is " + fact);
}
}
When you declare a variable inside a
for
loop, there is one important point to remember:
The scope of that variable ends when the
for
statement does. (That is, the scope of the variable
is limited to the
for
loop.) Outside the
for
loop, the variable will cease to exist. Thus, in the
preceding example,
Do'stlaringiz bilan baham: |