htmlbran86 htmlbran86 - 1 year ago 168 Question

Using Do While in a Multiplication App

I am editing a multiplication app in my VB class and I am setting up a calculation button to use a

Do While
statement to take what the user inputs and then display in a list box that number multiplied once per line (going from 1 to 9). Here is how my other function, using a For Next statement, looks when executed:

I need to emulate the same output when I code the
Do While
statement. Here's what I have:

Private Sub btnDoLoop_Click(sender As Object, e As EventArgs) Handles btnDoLoop.Click
' displays a multiplication table
Dim number, product, count As Integer
lstMultTable.Items.Add("Do Loop:")
Integer.TryParse(txtNumber.Text, number)
'Do Loop
count = 1
product = number * count
lstMultTable.Items.Add(" " & number.ToString() & " * " & count.ToString() & " = " & product.ToString())
Loop Until count = 10

End Sub

When I run this, my calculations are supposed to show in the list box, but it ends up locking up the program. Does anyone have any suggestions in regards to the
Do While
loop? Thanks in advance.

Answer Source

The count =1 should be before the loop. Inside theloop there should be count = count + 1 (after the calculation).

Your loop now is an infinite loop because count is always 1.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download