Changes

Jump to: navigation, search

C Sharp Looping - The for Statement

596 bytes added, 16:54, 17 January 2008
The C# ''for'' Loop
System.Console.WriteLine ("j = " + j);
 
</pre>
 
== C# Loop Variable Scope ==
 
A key point to note in creating loops is that any variables defined within the body of a loop is only visible to code within the loop. This is concept known as ''scope''. If, for example, a variable ''myCounter'' is defined in a for loop, that variable ceases to exist once the loop terminates:
 
<pre>
 
// variable myCounter does not yet exist
 
for (int i = 0; i < 10; i++)
{
int myCounter = 0; //myCounter variable created in scope of for loop
 
myCounter += i;
}
 
// after loop exit variable myCounter is now out of scope and ceases to exist
</pre>

Navigation menu