how to test a range of numbers

  • Thread starter Thread starter jcoon
  • Start date Start date
J

jcoon

I'm trying a test for below -10 and another test for 0 to -9.9 and a third
for above.
First and third test work corectly but I don't know how to test for the
second, range values between 0 and -9.99.

If SURFDIFF < -10 Then
StaElev(10) = "BELOW" 'LABEL BELOW ' works
End If

'how to you test a range between 0 and -9.99
If SURFDIFF < 0 and < -9.99 Then
StaElev(10) = "BELOW WITHIN 10 FT"
End If

If SURFDIFF > 0 Then
StaElev(10) = "ABOVE" 'GET LABEL ABOVE BELOW ' works
End If

Thank you

John
 
Well ... "between" means greater than some lower bound AND (hint!) less than
some upper bound. But in programming, where the smallest details matter,
you have to think about whether you want "greater than" or "greater than or
equal to", and "less than" or "less than or equal to".

The idiom I generally use, and I don't think there is much choice, is, for
example, ...

If (i >= -10) And (i <= 0) Then
MsgBox("It's between -10 and 0")
End If

Although a better message in this case would be "It's greater than or equal
to -10 and less than or equal to 0". (And I'm not sure if the parenthesizes
in the If statement are necessary - but they don't hurt.)

Also, think about the fact that if SURFDIFF is less than -10 then it can't
also be greater than 0. So once your logic has determined that it is less
than -10 it's a waste of the computer's precious time to see if it's also
greater than 0.

See if you can't structure your code so that it avoids the greater-than-0
test when SURFDIFF has already been determined to be less than -10. (You'll
need to use the Else part of the If-Then-Else statement.) Failure to do
this might lower your grade on this assignment by one letter grade.

Good Luck, Bob
 
Well ... "between" means greater than some lower bound AND (hint!) less than
some upper bound. But in programming, where the smallest details matter,
you have to think about whether you want "greater than" or "greater than or
equal to", and "less than" or "less than or equal to".

The idiom I generally use, and I don't think there is much choice, is, for
example, ...

If (i >= -10) And (i <= 0) Then
MsgBox("It's between -10 and 0")
End If

Although a better message in this case would be "It's greater than or equal
to -10 and less than or equal to 0". (And I'm not sure if the parenthesizes
in the If statement are necessary - but they don't hurt.)

Also, think about the fact that if SURFDIFF is less than -10 then it can't
also be greater than 0. So once your logic has determined that it is less
than -10 it's a waste of the computer's precious time to see if it's also
greater than 0.

See if you can't structure your code so that it avoids the greater-than-0
test when SURFDIFF has already been determined to be less than -10. (You'll
need to use the Else part of the If-Then-Else statement.) Failure to do
this might lower your grade on this assignment by one letter grade.

Good Luck, Bob


If SURFDIFF < 0 AndAlso SURDIFF > -9.99

You should use AndAlso instead of And (and OrElse instead of Or).

And always evaluates both expressions, AndAlso doesn't evaluate the
second if there is no need to.

Also be aware that values of -10 to -9.99 and 0 don't match any of
your tests.
 
Bob,

Thanks,
I'm sample in cad 500,000 + coordinates with elevation against airport
flight paths to determine if objects penetrate the approach surface. Surface
object is 3D surface model which I need only objects that penetrate the
surface and or are within 10 ft of the surface, all other are either 0 which
is not within the surface model or far enough below (values below -10) that
they are not used. With your help and others I can continue testing my
samples.

Thanks
John
 
Back
Top