If you know how many times it needs to be repeated (even if the number will come from a variable) you will use a fixed loop (For…Next).
If you don’t know how many times it needs to be repeated you will use a conditional loop (Loop Until or Do While).
If using a conditional loop, you’ll need to work out what will make the loop stop and create a suitable condition.
For...Next loops increment by 1 with each iteration but this can be changed using the Step keyword.
Example - For...Next
show
VB code |
Pseudocode |
Output |
For counter = 1 To 5 lstOutput.Items.Add (counter) Next |
FOR counter FROM 1 TO 5 SEND counter TO DISPLAY END FOR |
1 2 3 4 5 |
Example - For...Next
show
VB code |
Pseudocode |
Output |
For counter = 5 To 1 Step -1 lstOutput.Items.Add (counter) Next |
FOR counter FROM 5 TO 1 SEND counter TO DISPLAY END FOR |
5 4 3 2 1 |
Example – Loop Until
show
VB code |
Pseudocode |
counter=0 Do Stuff to be repeated goes here counter=counter+1 Loop Until counter=10 |
SET counter TO 0 REPEAT Stuff to be repeated goes here SET counter TO counter+1 DO LOOP UNTIL counter=10 |
VB code |
Pseudocode |
counter=0 Do While counter<10 Stuff to be repeated goes here counter=counter+1 Loop |
SET counter TO 0 WHILE counter<10 DO Stuff to be repeated goes here SET counter TO counter+1 END WHILE |