Sometimes it is necessary to repeat a section of code a set amount of times. The For..Next loop structure ias used to handle these problems. It has a "built in" counter which is automatically increased as the code is repeated.
You have been contracted to write a Visual Basic program that will calculate the total of the scores of the 11 players in the Wanneroo cricket team.

This program requires the For .. Next structure. The commands within the For..Next block are repeated 11 times. As the block repeats, the variable Count goes up from 1 to 11. The Next Count shows where the block of code ends.
Set Total To 0 For Count <- 1 To 11 do Get Score Set Total To Total + Score Next Count Write Total
Private Sub CmdProcess_Click()
Dim Total As Integer
Dim Score As Integer
Dim Count As Integer
Dim strCount as string
Total = 0
For Count = 1 To 11
'Inputbox returns a sting variable
strScore = InputBox("Enter Score for player "& Count)
'convert string to integer
score = CInt(strScore)
Total = Total + Score
Next Count
MsgBox("The total is " & Total)
End Sub
The For and Next have the part of the code that is repeated. Although Visual Basic does not care that it is indented, it is easier for the programmer to read if it is indented.
As the program is running, the user is asked to enter a score. The value of Count can be used as part of the Input Box message to show which the score is to be entered which will be Enter score for player 1, etc. As each score is entered, it is added to Total. This is shown in a message box after the 11 scores have been entered.
The form of the FOR..NEXT loop is
FOR Count_Var = Start_Val TO End_Val STEP Step_Val
Where: -
Count_Var is an Integer Variable
Start_Val, End_Val, Step_Val are literal Integer values, Integer Constants or Integer Variables
Step_Val can be either positive or negative.
In making a program to show a number of lines as output, a list box can be used.The following code shows how it can be used. The listbox name property is lstOut. No need to set any other properties.
For count = 6 To 15 Step 3
lstOut.Items.Add(count)
Next count
- Print the integers from 1 to 100.
- Print the even numbers from 2 to 200.
- Count down from 100 to 1
- Counts from any start number to any finish number counting up or down by any step size. Use Text Boxes to enter the start, finish and step number.
- Add the numbers from 1 to 100
- Print any number of smily faces :) required by the user.