The For loop is used when the number of repetitions is known. It uses a single variable to start the loop, and indicates how many times the loop needs to execute.
For intCount = 0 To 5
msgbox("This is loop number " & intCount)
Next
The loop counter variable in this instance is intCount. It begins at 0. Each pass of the loop causes the loop counter to increment +1. The loop will execute 6 time - from 0 to 6.
Loops allows a programmer to repeat a block of code a number of times while some condition is true, or until some condition becomes true. For instance, a loop could be used to prompt a user for an answer until the correct input is entered.
A loop must contain a starting condition and an ending condition. It is very important that a loop is able to reach an ending condition or we end up with what what is called an ‘infinite loop’ – a loop that never ends. This can cause our programs to behave abnormally and may even cause a computer to ‘crash’.