Results 1 to 5 of 5

Thread: Problem with Do Loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Problem with Do Loop

    This code suppose to take the user's input of current balnace, the annual interest rate, the current month, and amount he/she will pay per month. When the CALCULATE button is clicked the program starting with the current month, should compute and display for each month until the balance is reduced to zero, the month, the interest charge, payment, and new balance. But somehow im stuck in an infinite loop and its not displaying anything. please help me


    Code:
    Public Class frmPaySchedule
    
        Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalculate.Click
    
            
            Dim month As Integer = CInt(txtmonth.Text)
            Dim balance As Decimal = CDec(txtbalance.Text)
            Dim interest As Decimal = CDec(((txtAPR.Text / 100) / 12) * balance)
            Dim payment As Decimal = CDec(txtpayment.Text)
    
            Do Until balance = 0
                If month = 5 Or month = 6 Or month = 7 Or month = 8 Then
                    payment = payment + 100
                End If
                If month = 11 Then
                    payment = payment + 50
                End If
                balance = balance - (payment - interest)
                txtResult.Text = month.ToString & " " & FormatCurrency(interest) & " " & _
                                 FormatCurrency(payment) & " " & FormatCurrency(balance)
    
                month += 1
    
            Loop
    
    
    
        End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with Do Loop

    As far as displaying the results, you're setting the Text of the TextBox each time so you're going to overwrite the previous data each time. If you want to show every month then you need to append the data, not replace.

    As for the infinite loop, your exit condition is (balance = 0), so obviously that is not being met. Maybe you are overshooting by a tiny amount. To find out, you need to debug your code. Place a breakpoint at the start of the loop with the F9 key. When execution breaks at that line, you can step line by line using the F10 key. At each step you can use the Autos, Locals, Watch and Immediate windows to evaluate basically anything to test whether what you expect to happen actually happened. As soon as things don't match you know that you've found an issue, either in your logic or in your code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: Problem with Do Loop

    This is how its suppose to look like, but is using the Do Loop the right approach to obtain that result?
    Attached Images Attached Images  

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with Do Loop

    Do and While loops are used to do the same thing multiple times when the actual number is unknown and defined by when a particular condition is satisfied. That's exactly what you want so yes, a Do loop is a good option.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: Problem with Do Loop

    yea i changed it to a label box, but looking from the results the exit statement "do until balance = " cannot be 0, should it be set as a decimal or currency or something else for the loop to work, thats what im having trouble with.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width