Android Question Getting value based a negative number

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to do something basic, but can't work out what I am doing wrong (one of those days).

B4X:
Dim value1 As Int = -71
    
    If (value1 >= -1) And (value1 <= -70) Then
        Log("EXCELLENT")
    Else If (value1 >= -71) And (value1 <= -85) Then
        Log("GOOD")
    Else If (value1 >= -86) And (value1 >= -100) Then
        Log("FAIR")
    Else If (value1 >= -101) And (value1 >= -140) Then
        Log("POOR")
    End If

I am trying to make it log the correct value based on value1.

In the above example it should log 'Good' since it's between -71 and -85. However it logs 'Fair' when I run the above code.

I am thinking it has something with the number being a negative number.

Anyone know what I am doing wrong. Should I be doing this another way ?
 

PaulMeuris

Active Member
Licensed User
Maybe you could try this:
B4X:
    Dim value1 As Int = -75
    If (value1 <= -1) And (value1 >= -70) Then
        Log("EXCELLENT")
    Else If (value1 <= -71) And (value1 >= -85) Then
        Log("GOOD")
    Else If (value1 <= -86) And (value1 >= -100) Then
        Log("FAIR")
    Else If (value1 <= -101) And (value1 >= -140) Then
        Log("POOR")
    End If
First check for less than or equal to and then check for greater than or equal to.
This is because the numbers are negative.
The range is from -1 to -140. All other numbers are not checked.
-75 reports GOOD.
 
Upvote 0

emexes

Expert
Licensed User
I'm a bit late to the party, but... you could halve your workload and decimate the probability of range gap or overlap bugs with:

B4X:
Dim value1 As Int = -75
If value1 >= 0 Then
    '''Log("HOLY SMOKE")
Else If value1 >=  -70 Then    '  -1 to  -70
    Log("EXCELLENT")
Else If value1 >=  -85 then    ' -71 to  -85
    Log("GOOD")
Else If value1 >= -100 Then    ' -86 to -100
    Log("FAIR")
Else If value1 >= -140 Then    '-101 to -140
    Log("POOR")
End If

plus bonus of same code will still work if you change value1 to be floating point rather than integer.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is another variant comparable to @emexes using CASE.:
B4X:
Dim value1 As Int = -75
    Select True
    Case  value1 >=  0
            Log("HOLY SMOKE")
    Case  value1>=  -70     '  -1 to  -70
        Log("EXCELLENT")
    Case value1 >=  -85     ' -71 to  -85
        Log("GOOD")
    Case  value1>= -100     ' -86 to -100
        Log("FAIR")
    Case Else  value1>= -140     '-101 to -140
        Log("POOR")
    End Select
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…