counterwith a value of
0. This counter will hold how many times we have run the loop.
while) followed by round brackets that contain the loop condition.
boolor Boolean value. If the condition is true, our program enters the loop body, the part surrounded by the curly brackets. The statements in the loop body will then be executed.
Console.WriteLine("END")). Instead, it will go back to the condition again and test if the condition is still true. It will keep going back to the loop body until the loop condition results in a
counter++is shorthand for
counter = counter + 1. We are increasing the value of
counterby 1, then assigning that value to
counterso it's easier to see how the loop is working.
counterincreasing after every iteration of the loop.
10, the program tests the condition
counter < 10for the last time. Because 10 is not less than 10, the condition is now false. The program exits the loop body and goes to the next statement outside, which is
counter++and run the code, the condition
counter < 10will never become false. Our program will enter an infinite loop!
countervariable separately anymore (Line 1 in our original program) - it is now declared within the for loop, inside the round brackets.
counter < 10.
counter- is the last expression inside the round brackets.