If then problem

DekaFlash

Well-known member
Joined
Feb 14, 2006
Messages
117
Programming Experience
1-3
I have an if then else statement, which is used to perform calculations except in some cases its going to a branch it shouldn't do.

Dim
BackOdds As Decimal
BackOdds = CDec(SubmitBackOdds.Text)
MsgBox("Back Odds " + BackOdds.ToString)

If
(BackOdds > 2 Or BackOdds < 3) Then
MsgBox("SBO 700 " + SubmitBackOdds.Text)
OddsReminder = BackOdds
Mod CDec(0.02)
ElseIf (BackOdds > 3 Or BackOdds < 4) Then
MsgBox("SBO 703 " + SubmitBackOdds.Text)
OddsReminder = BackOdds
Mod CDec(0.05)
ElseIf (BackOdds > 4 Or BackOdds < 6) Then
MsgBox("SBO 706 " + SubmitBackOdds.Text)
OddsReminder = BackOdds
Mod CDec(0.1)
ElseIf (BackOdds > 6 Or BackOdds < 10) Then
MsgBox("SBO 709 " + SubmitBackOdds.Text)
OddsReminder = BackOdds
Mod CDec(0.2)
ElseIf (BackOdds > 10 Or BackOdds < 20) Then
MsgBox("SBO 712 " + SubmitBackOdds.Text)
OddsReminder = BackOdds
Mod CDec(0.5)
ElseIf (BackOdds > 20 Or BackOdds < 30) Then
MsgBox("SBO 715 " + SubmitBackOdds.Text)
OddsReminder = BackOdds
Mod CDec(1)
ElseIf (BackOdds > 30 Or BackOdds < 50) Then
MsgBox("SBO 718 " + SubmitBackOdds.Text)
OddsReminder = BackOdds
Mod CDec(2)
ElseIf (BackOdds > 50 Or BackOdds < 100) Then
MsgBox("SBO 721 " + SubmitBackOdds.Text)
OddsReminder = BackOdds
Mod CDec(5)
ElseIf (BackOdds > 100 Or BackOdds < 1000) Then
MsgBox("SBO 724 " + SubmitBackOdds.Text)
OddsReminder = BackOdds
Mod CDec(10)
End If
 
First If case (BackOdds > 2 Or BackOdds < 3) spans all numbers, it should probably be AND.

Select Case is more readable for ranges, example:
VB.NET:
select case backodds
case 2 to 4
 
case 4 to 10
 
case 11 to 100
 
end select
 
Just think about this for a second..

What are the chances that a decimal number is greater than 2, or less than 3

me> Pick a number
you> 1
me> is it greater than 2?
you> no
me> is it less than 3?
you> yes
me> okay, do plan A

me> Pick a number
you> 9999999
me> is it greater than 2?
you> yes
me> okay, do plan A


Plan A will always be done! d>2 or d<3 is ALWAYS TRUE for any value of d



When doing if elses in block like this, you have to proceed from the most specific, to the most generic. Always true is the most generic.. The widest range, in other words. The smallest range, is most specific.

In evaluating specificness, you do not consider the individual elements.. ie you have written here 2 and 3, both small number with a small range between them but overall the consequences of their combination are huge.. that is what you should be looking at when writing your IFs

Take another look at your ifs.. all of them. Are any more specific than any other?

Can you think of a number that is greater than 100 or less than 1000 ? Every number I can think of is true for that.. Can you think of any that arent (greater than 100 or less than 1000).. When you think some more, you'll see that all your IFs evaluate to true regardless of the value.. So only ever the first one will be run..
 
Back
Top