Let’s look at this loop:
For N = 1 To 10
TextWindow.WriteLine(N + ” x 9 = ” + (N * 9))
Now let’s take a look at a diagram to explain this!
Figure: Illustrating the operation of the For loop
Let’s explain what’s going on behind the scenes:
First N is set to 1, the starting value.
The value of N is compared with the ending value (or terminal value) of the loop, which is 10 in this case. If N is greater than 10, the loop ends, and the program moves to the statement after the EndFor keyword.
If N is less than or equal to the ending value, the program moves inside the For loop and runs the statements in its body. When the program reaches the EndFor keyword, it adds one to N and repeats step 2.
Eventually N becomes more than 10, and the loop ends. Your program moves out of the loop to the statement after EndFor. After the loop ends, the value of N is 11.
Note: The For loop’s normally used when you know the number of iterations in advance. The terminal condition’s checked right after the initialization and at the beginning of each iteration. If this condition’s true, your program runs the code in the loop. If it’s false, the loop ends, and the program moves to the statement after the EndFor keyword.
Do you have any questions? Ask us! We’re full of answers and other fine things!
Head to the Small Basic forum to get the most answers to your questions:
And go to http://blogs.msdn.com/SmallBasic to download Small Basic and learn all about it!
Small and Basically yours
– Ninja Ed & Majed Marji