Hi im new to C# as you might have guessed..
But I'm having trouble understanding the loops statement.
for (int i = 0; i <= 10; i++)
{
{
//Console.WriteLine(i.ToString());
}
}
I don't understand what is going on here, i just know the outcome. Could someone please help me.
Loading
AlanPosted Apr 22, 2008, 6:34 AM
The for statement has three sections separated from each other by semi-colons.
The first section defines what's called a loop control variable 'i' as an integer and gives it an initial value of zero. This variable only has meaning within the for statement, not outside.
The second section carries out a test and, if the test is true, the statement(s) in the body of the for statement are executed. If the test is false, then the for loop ends and the statement after it is executed. The test here is that 'i' is less than or equal to 10.
The third section increases the loop control variable by one, after the loop has executed.
So, here, we start off with i == 0 which is less than 10 and so the loop executes.
'i' is then increased by 1 which is still less than 10 and so the loop executes again.
We then carry on like this until 'i' reaches a value of 11 which is greater than 10 and so the loop ends.
At the end of all this, you should see the numbers, 0 to 10 inclusive, listed on separate lines on the console.