Shop OBEX P1 Docs P2 Docs Learn Events
Using IF to Evaluate a Range of Values? — Parallax Forums

Using IF to Evaluate a Range of Values?

coryco2coryco2 Posts: 107
edited 2014-03-13 16:10 in Propeller 1
I know you can use CASE to evalaute a range of values, like this:
CASE SomeValue
     10..20 : Result1
     21..40 : Result2
     OTHER: Result3

But I'm wondering if there is some similar syntax for evaluating ranges with IF as well? The Propeller Tool won't allow this, for instance:
IF SomeValue == 10..20
     Result1
ELSEIF SomeValue == 21..40
     Result2
ELSE
     Result3
Though it appears it should be the same. (Btw, I did also try putting the ranges in brackets, parenthesis, etc. with no success). Can anyone tell me what the problem is?

Comments

  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-03-13 11:16
    No, the case structure allows you to simplify complex if/else constructs. Your if/else code should look like this.
    if ((somevalue => 10) and (somevalue =< 20))
      result1
    elseif ((somevalue => 21) and (somevalue =< 40))
      result2
    else
      result3
    


    Why? That's the syntax definition -- if you don't like it, do what others have and create your own language. ;)
  • coryco2coryco2 Posts: 107
    edited 2014-03-13 11:28
    Ok, thanks. It does seem a little inconsistent, though...
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-03-13 12:39
    The fact of the matter is that case came after if, hence has what some consider cleaner syntax. There is nothing stopping you from creating your own computer language with what you cosider consistent syntax. The if/elseif syntax of Spin is consistent with other languages.
  • Heater.Heater. Posts: 21,230
    edited 2014-03-13 13:27
    coryco2,
    It does seem a little inconsistent, though...
    In what way inconsistent?

    Clearly "if a = b " is asking if one thing has the same value, is equal to, an another thing. Does 2 = 2?, yes. Does 2 = 4?, no.

    This is clear to anyone who has done maths at school.

    If you want to ask a different question , like is 3 in the set of numbers {1, 2, 3, 4, 5} then you need a different notation. Enter the "case".
  • coryco2coryco2 Posts: 107
    edited 2014-03-13 15:27
    It is inconsistent that IF and CASE cannot use the same syntax to indicate a range of numbers to be tested by the operation. As JonnyMac points out, this was a choice of the language designer. I was merely making an observation, however. I think Spin is quite useful. :smile:
  • kuronekokuroneko Posts: 3,623
    edited 2014-03-13 15:38
    @coryco2: you could always use (at a cost):
    if lookdown(SomeValue: 10..20)
      ' index 1..10, i.e. TRUE, do something
    
    Out of range evaluates to 0 (FALSE).
  • Heater.Heater. Posts: 21,230
    edited 2014-03-13 16:10
    coryco2,
    It is inconsistent that IF and CASE cannot use the same syntax to indicate a range of numbers to be tested by the operation.
    I kind of see what you mean.

    It's only that I'm old fashioned and I think "=" should mean "equals" as it has done in maths for a long time.

    What you are asking for is an operator that tests for something being a member of a set or not. That should be a different operator to my mind.

    In fact in mathematics there has been such an operator for a long time: If a is member of B, this is denoted a ∈ B

    So your "if" test would be something like:

    if SomeValue ∈ { 1, 2, 3, 4, 5 }

    I just feel we should not break decades, or even centuries, of notation and semantics on a whim.
Sign In or Register to comment.