The Iteration Lab

Or

Multiplying 3 Integers….the Hard Way

 

 

This lab is designed to be a simple demonstration of nested iteration using the Do While Loop construct in visual basic. The “product” of three integers entered by the user is calculated by iterated summing of the number 1 (aka counting). The numbers entered by the user to be multiplied are used as termination conditions in the nested loops.

Items to note:

·        The numbers entered by the user to be multiplied are used as termination conditions in the nested loops.

·        The repeated summing works only with integers.

·        The first statement in the loop body increments the iterate variable

·        The iterate variables are explicitly initialized

 

Things to try:

·        Very large numbers

·        Letters

·        No entries

·        Floating point numbers

 

Changes you can make:

·        Implement validity checking

·        Modify the program to take 4 numbers

 

 


 

Iteration Lab

 

 

Private Sub cmdStartIteration_Click()

Dim intNumA As Integer

Dim intNumB As Integer

Dim intNumC As Integer

 

Dim intCountA As Integer

Dim intCountB As Integer

Dim intCountC As Integer

 

 

Dim intRunningSum As Integer

intRunningSum = 0

 

intNumA = txtNumA

intNumB = txtNumB

intNumC = txtNumC

 

intCountA = 0

Do While intCountA < intNumA

    intCountA = intCountA + 1

    intCountB = 0

    Do While intCountB < intNumB

        intCountB = intCountB + 1

        intCountC = 0

        Do While intCountC < intNumC

            intCountC = intCountC + 1

            intRunningSum = intRunningSum + 1

        Loop

    Loop

Loop

lblAnswer.Caption = Val(intRunningSum)

Me.BackColor = vbBlue

 

           

End Sub

 

Private Sub Form_Load()

txtNumA.Text = ""

txtNumB.Text = ""

txtNumC.Text = ""

lblAnswer.Caption = ""

 

 

 

End Sub