Chapter 6 Repetition - Fixed Loop

Fixed Loop Structure - For .. Next Structure

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.

PROBLEM

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.

PLANNING

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.

Pseudocode

Set Total To 0
For Count <- 1 To 11 do
  Get Score
  Set Total To Total + Score
Next Count
Write Total

CODING

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.

Counting by more than 1

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.

Using a List box for output (note multi lines in text boxes is difficult)

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

  • EXERCISE 6 - complete question 1 then describe how you would go about One of 2, 3 or 4. Do not code it.

    1. Programs are required to perform the tasks indicated. First make the plans for each of the programs indicated. (Do not write the programs). Then create one program to demonstrate the plans from above.
    1. Print the integers from 1 to 100.
    2. Print the even numbers from 2 to 200.
    3. Count down from 100 to 1
    4. 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.
    5. Add the numbers from 1 to 100
    6. Print any number of smily faces :) required by the user.
    1. You have been contracted to write a program that will accept from the user the rainfall (in Millimeters) for each day of the week and then show total and average rainfall for that week.
    2. Write a program that will show a list of the temperatures from 0-20 degrees celcieus in farenheit
    3. Factorial values of a number are calculated by multiplying the number, and all the numbers before it, together. So 5!=5x4x3x2x1. Write a program that will calculate the factorial numbers from 2 to 10 and display them in a text box.

    Author: Mike Leishman Last Updated 28 June 1998