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
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.
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.
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.