do while

Z

zymotic

Hi,

I'm just starting out with vb.net and programming in general. Can
someone tell me why that when i run this code and enter any number
into txtAnnualSal (other than 0) that i get an infinite loop
happening?

Private Sub btnCalcTax_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCalcTax.Click
Dim annual_sal, income_tax As Double
annual_sal = CDbl(txtAnnualSal.Text)
Do While annual_sal <> 0
calculate_tax(annual_sal, income_tax)
txtTax.Text = FormatCurrency(income_tax)
annual_sal = CDbl(txtAnnualSal.Text)
Loop
MsgBox("Yearly salary must be greater than $0")
End Sub
Sub calculate_tax(ByVal annual_sal As Double, ByRef income_tax As
Double)
If annual_sal > 0 And annual_sal <= 5000 Then
income_tax = 0
End If
If annual_sal > 5000 And annual_sal <= 10000 Then
income_tax = annual_sal * 0.06
End If
If annual_sal > 10000 And annual_sal <= 20000 Then
income_tax = annual_sal * 0.15
End If
If annual_sal > 20000 And annual_sal <= 30000 Then
income_tax = annual_sal * 0.2
End If
If annual_sal > 30000 And annual_sal <= 40000 Then
income_tax = annual_sal * 0.25
End If
If annual_sal > 40000 Then
income_tax = annual_sal * 0.3
End If
End Sub
End Class
 
J

Jerry Ham

Sure, you are setting annual_sal back to its original, non-zero value just
before the loop statement...

Jerry
 
A

Armin Zingler

zymotic said:
I'm just starting out with vb.net and programming in general. Can
someone tell me why that when i run this code and enter any number
into txtAnnualSal (other than 0) that i get an infinite loop
happening?

Private Sub btnCalcTax_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCalcTax.Click
Dim annual_sal, income_tax As Double
annual_sal = CDbl(txtAnnualSal.Text)
Do While annual_sal <> 0
calculate_tax(annual_sal, income_tax)
txtTax.Text = FormatCurrency(income_tax)
annual_sal = CDbl(txtAnnualSal.Text)
Loop

You never exit the loop. annual_sal is never <>0 because, within the loop,
you always set it to the value of the textbox content.
 
G

Guest

Hi:

You probably would want to use an If/Then statement,
because it appears that you are only wanting to execute
the code once in the Do While Loop.

Robert
 
O

One Handed Man [ OHM# ]

Also,

A more elegent way of doing these annual tax decicsions would be to use the
select case statement rather than using a mollion IF statements: IE

Select Case tax
Case Is < 0
'Do stuff
Case 0 To 4
'Do stuff
Case 5 To 9
'Do stuff
Case Is > 9
'Do stuff
End Select

Regards - OHM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top