Ross Sheppard High School
Computer Programming 20
Student Learning Guide
Controlling Program Flow with Looping Structures

The Loop Program


  When you have finished this project, you will be able to:
create a Do . . . Loop
describe how a Do . . . Loop works and when it should be used
create a For . . . Next loop
describe how a For . . . Next loop works and when it should be used
  1. Create a new folder called Loop on your personal drive.

  2. Start Visual Basic and create a new project. Save the form and Project as Loop.

  3. Add your programmer information and Option Explicit information to the beginning of the program.

  4. The first looping structure that you will learn is the Do . . . Loop. A Do . . . Loop executes a set of statements as long as a condition is true.

  5. You will create a prime number application to learn how to create a Do . . . Loop. You will recall that a prime number is a number that is divisible only by 1 and itself. 2, 3, and 7 are prime numbers, but 4, 6, and 9 are not because they are able to be divided by more than 1 and themselves.

  6. Add the neceesary objects to your form using the Objects Table below.

    Objects Table
    Object Property Setting
    Form Name frmLoop
      Caption The Loop Program
    TextBox Name txtInteger
      Text No text
    Label Name lblEnterInteger
      Caption Enter an integer:
    Label Name lblPrimeResult
    Command Button Name btnTest
      Caption Check Test
    Command Button Name btnExit
      Caption Exit


  7. Write the code for the Test button as illustrated below.



  8. Save your program and run it. Try several numbers to find out if they are prime numbers or not.

  9. The next type of loop that you will learn is a For . . . Next loop. Unlike the Do . . . Loop, a For . . . Next loop continues to run until a counter reaches an ending point. To learn how to create a For . . . Next loop, you will create a factorial progam. A factorial program is the product of all of the positive integers from 1 to the ending number. For example, 5 factorial is written as 5!, and means 5 * 4 * 3 * 2 * 1 for a product of 120.

  10. Add the objects required for the Factorial Program to your Loop form. Use the Object Table below.

    Object Table
    Object Property Setting
    TextBox Name txtNumber
      Text No text
    Label Name lblEnterNumber
      Caption Enter a number:
    Label Name lblFactorialMessage
    Label Name lblFactorial
    Command Button Name btnComputeFactorial
      Caption Computer Factorial


  11. The code for the Compute Factorial button is shown below.



  12. Click here to see the marking guide for this project.

  13. When you have finished, send an email to your teacher asking that this project be marked.


© 1998-2000 N.F. Mathew, EdD
File name: Loop.htm
Last updated on November 3, 2000