hey guys,

i'm working on this assignment in where I have to take a For...Next Loop and turn it into a Do While Loop. I think I've kinda got the coding down but when I try to calculate the rates the program freezes up. I got the code here, any suggestions on what next because the code doesnt show any errors so i must have mixed something up or something.

thanx

For...Next

Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
        'calculates the monthly payments on a loan using
        'terms of 3, 4, and 5 years and interest rates of 5% - 10%
        Const TermHeading As String = _
            "          3 yrs          4yrs          5yrs"

        Dim principal As Double
        Dim monthlyPayment As Double
        Dim isConverted As Boolean

        Me.xPaymentsLabel.Text = String.Empty
        isConverted = _
            Double.TryParse(Me.xPrincipalTextBox.Text, principal)

        If isConverted Then
            'display the term in the heading
            Me.xPaymentsLabel.Text = TermHeading _
            & ControlChars.NewLine

            'calculate and display payments
            For rate As Double = 0.05 To 0.1 Step 0.01
                Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                & rate.ToString("P0") & ""

                For term As Double = 3.0 To 5.0
                    monthlyPayment = _
                    -Financial.Pmt(rate / 12.0, _
                    term * 12.0, principal)
                    Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                      & monthlyPayment.ToString("N2") & ""

                Next term

                Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                  & ControlChars.NewLine

            Next rate
        Else 'principal cannot be converted to a number
            MessageBox.Show("Please re-enter the principal. ", _
            "Payment Calculator", MessageBoxButtons.OK, _
            MessageBoxIcon.Information)
        End If

        Me.xPrincipalTextBox.Focus()
        Me.xPrincipalTextBox.SelectAll()

    End Sub
End Class

The Do...While that I'm trying to make work

Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
        'calculates the monthly payments on a loan using
        'terms of 3, 4, and 5 years and interest rates of 5% - 10%

        Const TermHeading As String = _
            "          3 yrs          4yrs          5yrs"

        Dim principal As Double
        Dim monthlyPayment As Double
        Dim rate As Double
        Dim term As Double

        Dim isConverted As Boolean

        isConverted = _
            Double.TryParse(Me.xPrincipalTextBox.Text, principal)

        Do
            'calculate and display payments
            Do While rate <= 0.1


                If isConverted Then
                    'display the term in the heading
                    Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                    & rate.ToString("P0") & " "
                    Me.xPaymentsLabel.Text = TermHeading _
                      & ControlChars.NewLine
                ElseIf rate >= 3.0 And rate <= 5.0 Then
                    monthlyPayment = _
                    -Financial.Pmt(rate / 12.0, _
                      term * 12.0, principal)
                    Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                        & monthlyPayment.ToString("N2") & " "
                    Me.xPaymentsLabel.Text = TermHeading _
                      & ControlChars.NewLine

                ElseIf CBool(term) Then
                    Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                        & ControlChars.NewLine

                Else 'principal cannot be converted to a number
                    MessageBox.Show("Please re-enter the principal. ", _
                    "Payment Calculator", MessageBoxButtons.OK, _
                    MessageBoxIcon.Information)
                    Exit Do
                End If
            Loop
        Loop Until Me.xPaymentsLabel.Text = String.Empty


        Me.xPrincipalTextBox.Focus()
        Me.xPrincipalTextBox.SelectAll()

    End Sub
End Class

Once again, thanx for any help/suggestions.

>> Do While rate <= 0.1
You have to set rate to value of 0.05 before the above line. The way you have it now that variable just contains any old random value which might or might not be less than 0.01.

Just before the end of that loop you have to increment rate by a value of 0.01.

>> Do While rate <= 0.1
You have to set rate to value of 0.05 before the above line. The way you have it now that variable just contains any old random value which might or might not be less than 0.01.

Just before the end of that loop you have to increment rate by a value of 0.01.

cool, thanx.

I'll try that now and let you know what happened

working with this now. I'll let you know what happens

'calculates the monthly payments on a loan using
        'terms of 3, 4, and 5 years and interest rates of 5% - 10%

        Const TermHeading As String = _
            "          3 yrs          4yrs          5yrs"

        Dim principal As Double
        Dim monthlyPayment As Double
        Dim rate As Double = 0.05
        Dim term As Double

        Dim isConverted As Boolean

        isConverted = _
            Double.TryParse(Me.xPrincipalTextBox.Text, principal)

        Do
            'calculate and display payments
            Do While rate <= 0.1


                If isConverted Then
                    'display the term in the heading
                    Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                    & rate.ToString("P0") & " "
                    Me.xPaymentsLabel.Text = TermHeading _
                      & ControlChars.NewLine

                    rate = rate + 0.01

                ElseIf rate >= 3.0 And rate <= 5.0 Then
                    monthlyPayment = _
                    -Financial.Pmt(rate / 12.0, _
                      term * 12.0, principal)
                    Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                        & monthlyPayment.ToString("N2") & " "
                    Me.xPaymentsLabel.Text = TermHeading _
                      & ControlChars.NewLine

                    rate = rate + 0.01


                ElseIf CBool(term) Then
                    Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                        & ControlChars.NewLine

                Else 'principal cannot be converted to a number
                    MessageBox.Show("Please re-enter the principal. ", _
                    "Payment Calculator", MessageBoxButtons.OK, _
                    MessageBoxIcon.Information)
                    Exit Do
                End If
            Loop
        Loop Until Me.xPaymentsLabel.Text = String.Empty


        Me.xPrincipalTextBox.Focus()
        Me.xPrincipalTextBox.SelectAll()

    End Sub
End Class

Just before the end of that loop you have to increment rate by a value of 0.01.

Okay, i've changed the structure somewhat. here it is below:

Const TermHeading As String = _
            "          3 yrs          4yrs          5yrs"

        Dim principal As Double
        Dim monthlyPayment As Double
        Dim rate As Double = 0.05
        Dim term As Double

        Dim isConverted As Boolean

        isConverted = _
            Double.TryParse(Me.xPrincipalTextBox.Text, principal)

        Do
            If isConverted Then
                'display the term in the heading
                Me.xPaymentsLabel.Text = TermHeading _
                  & ControlChars.NewLine

                'calculate and display payments
                Do While rate <= 0.1
                    Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                    & rate.ToString("P0") & " "

                    rate = rate + 0.01

                Loop



                Do While rate >= 3.0 And rate <= 5.0
                  monthlyPayment = _
                    -Financial.Pmt(rate / 12.0, _
                      term * 12.0, principal)
                  Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
                        & monthlyPayment.ToString("N2") & " "

                    Me.xPaymentsLabel.Text = TermHeading _
                      & ControlChars.NewLine

                    rate = rate + 0.01
                Loop


            Else 'principal cannot be converted to a number
                MessageBox.Show("Please re-enter the principal. ", _
                "Payment Calculator", MessageBoxButtons.OK, _
                MessageBoxIcon.Information)
                Exit Do
            End If
        Loop Until Me.xPaymentsLabel.Text = String.Empty


        Me.xPrincipalTextBox.Focus()
        Me.xPrincipalTextBox.SelectAll()

    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.