Nick Balistreri
Nick Balistreri

Reputation: 131

For Next Loops displaying incorrect value

The Code has two options. Option one starts with 1 dollar and doubles everyday for 10 days. If this is correct the value should be $1024 but instead it shows double that ($2046)

The second option starts with $100 and adds $100 per day for 10 days. The number should come out to an even $1000 but instead shows $6500

Public Class Form1
    Private Sub compareButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles compareButton.Click

        Dim initValue1 As Integer = 1
        Dim value1 As Integer
        Dim initValue2 As Integer = 100
        Dim value2 As Integer

        Integer.TryParse(Option1TextBox.Text, value1)
        Integer.TryParse(Option2TextBox.Text, value2)



        For day As Integer = 1 To 10
            initValue1 = initValue1 * 2
            value1 += initValue1
        Next day

        Option1TextBox.Text = value1.ToString("C2")

        For day As Integer = 1 To 10
            initValue2 = initValue2 + 100
            value2 += initValue2
        Next day

        Option2TextBox.Text = value2.ToString("C2")

    End Sub
End Class

I feel like I am making a very small mistake. Any help?

Upvotes: 0

Views: 76

Answers (1)

Vinod
Vinod

Reputation: 4882

Try this:
No Need of value1 += initValue1 and value2 += initValue2 remove them and try


Dim initValue1 As Integer = 1
        Dim value1 As Integer
        Dim initValue2 As Integer = 100
        Dim value2 As Integer
        Integer.TryParse(Option1TextBox.Text, value1)
        Integer.TryParse(Option2TextBox.Text, value2)
        For day As Integer = 1 To 9
            initValue1 = initValue1 * 2
        Next
        Option1TextBox.Text = initValue1.ToString("C2")
        For day As Integer = 1 To 9
            initValue2 = initValue2 + 100
        Next
        Option2TextBox.Text = initValue2.ToString("C2")

enter image description here

Upvotes: 2

Related Questions