CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Help with Loop

  1. #1
    Join Date
    Sep 2010
    Posts
    6

    Help with 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
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Help with Loop

    Find this line in your code:

    Code:
          If month = 5 Or month = 6 Or month = 7 Or month = 8 Then
    Press F9 to create a BREAKPOINT on that line. Run it again. Now it will stop the first time it hits that line.

    Press control-G, and type ? month <enter> to see the value

    or add the line after the breakpoint:
    Code:
      Debug.Print month
    and it will print it there every time.

    Then, pressing F8 will step thru the program, line by line.

    Hint: Remove the breakpoint, but KEEP the line to Debug.Print. Watch what happens when you run the program!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2009
    Posts
    596

    Re: Help with Loop

    Definitly Go with the break point stuff DG says as that is a good way to debug progams. But from what I can tell by just looking at it as that Your loop condition is Do something until balance = 0. So balance never equals zero so that's why you're in the endless loop. And I'm sure you knew that. The conditional stuff is doing it's thing. Its decementing values. But it never gives you the exact 0 you need to terminate the loop. Maybe what you want to say is if balance equals zero or if balance is less than zero. You probably got onto the negative side of the number line and since balance isn't zero it just keeps going.

  4. #4
    Join Date
    Dec 2009
    Posts
    596

    Re: Help with Loop

    Misprint: You're not decementing in the conditonal block ifs but you know what i mean.

  5. #5
    Join Date
    Sep 2010
    Posts
    6

    Re: Help with 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  

  6. #6
    Join Date
    Dec 2009
    Posts
    596

    Re: Help with Loop

    The loop isn't the problem. You're always clobbering the textbox with your new value after every iteration. Try using a listview to get the nice organized look. Or try a the listbox control which is more simple but you won't get the nice grid look.

  7. #7
    Join Date
    Sep 2010
    Posts
    6

    Re: Help with 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.

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Help with Loop

    Did you do what I said? I guess not.

    You would have seen the problem, when Balance is LESS THEN 0. You can also say LESS THAN OR EQUAL TO.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help with Loop

    There are more than one problem. First the payment is increasing during the loop and second the loop condition balance=0 is not being met.

    and an "or balance < 0" to the loop condition to break out of the loop.

    Also if it is not your intention to add 100 to your payment amount in 4 of the months and another 50 in month 11 then you need to reset the payment far within your loop as it is the payment would get pretty steep by the end.

    If your starting payment was 50 then when you get to month 5 it would be 150 then 250 then 350 and so on. If this is the intent then it is fine but if not....
    Always use [code][/code] tags when posting code.

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

Featured