2 Replies - 622 Views - Last Post: 17 October 2013 - 05:26 AM Rate Topic: -----

#1 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 01-March 12

For loop and Do loop

Posted 17 October 2013 - 05:19 AM

I am experimenting with this code as I'm learning. This is a working code:
Public Class Form1
    Dim groupNumberInteger As Integer
    Dim salesInteger As Integer
    Dim totalinteger(7) As Integer

    Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
        Try
            'convert input group number to subscript
            With Me
                groupNumberInteger = Integer.Parse(groupTextBox.Text) - 1
                If groupNumberInteger >= 0 And groupNumberInteger <= 7 Then
                    'add sale to correct total
                    salesInteger = Integer.Parse(salesTextBox.Text)
                    totalinteger(groupNumberInteger) += salesInteger
                Else
                    MessageBox.Show("Enter a valid group number (1-8)", "Data Entry Error", _
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End With
        Catch
            MessageBox.Show("Numeric entries required for both group number and sales", _
                            "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End Try
    End Sub

        Private Sub totalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalButton.Click
        Dim outputString As String
        outputString = ""
        For indexInteger = 1 To 8
            outputString += "Group " & indexInteger & ": " & _
                     ControlChars.NewLine
        Next indexInteger

        MessageBox.Show(outputString, "Total of Each Group", _
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Sub
End Class


I am trying to change the last part of the code which is the for loop to a do loop. This is what I came up with:
        Private Sub totalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalButton.Click
        Dim outputString As String
        Dim indexInteger As Integer = 1
        outputString = ""
        Do
            outputString += "Group " & indexInteger & ": " & _
                     ControlChars.NewLine
        Loop Until indexInteger = 8

        MessageBox.Show(outputString, "Total of Each Group", _
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Sub


There is no errors but it doesn't seem to be working. I need help!

Is This A Good Question/Topic? 0
  • +

Replies To: For loop and Do loop

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: For loop and Do loop

Posted 17 October 2013 - 05:21 AM

You need to increment the variable indexInteger by one each time around the loop. This is done automatically in the for loop but not in the do loop.
Was This Post Helpful? 1
  • +
  • -

#3 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 01-March 12

Re: For loop and Do loop

Posted 17 October 2013 - 05:26 AM

Working! Thank you very much!!

This post has been edited by magicm00n: 17 October 2013 - 05:28 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1