Shop OBEX P1 Docs P2 Docs Learn Events
case expression — Parallax Forums

case expression

mikeamikea Posts: 283
edited 2015-02-19 12:24 in Propeller 1
Hi, I'm trying to use case to test 3 values for not equal to x. If 4 is not equal to x, then dira[4]~ and so on. The compiler wants to see an expression term and highlights "<>". I'm not clear on what an expression term is. Thanks

case  <> x
   4: dira[4]~
   5: dira[5]~
   6: dira[6]~

Comments

  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2015-02-18 20:01
    mikea wrote: »
    Hi, I'm trying to use case to test 3 values for not equal to x. If 4 is not equal to x, then dira[4]~ and so on. The compiler wants to see an expression term and highlights "<>". I'm not clear on what an expression term is. Thanks

    case  <> x
       4: dira[4]~
       5: dira[5]~
       6: dira[6]~
    

    That's a back to front case statement if I've seen one. Just supply the argument to case as x ( case x ) then that should work. However you could just as easily say:
    if x <> 4 or x <> 5 or x <> 6
    do this

    But I see you probably need a case statement because of the different actions but I'm not getting the mixed up logic of this operation as if x <> 4 could just as easily be x <> 5 ..... oh well.
  • tonyp12tonyp12 Posts: 1,951
    edited 2015-02-19 06:56
    Case is like TEST, you ask it for a RESULT, you can have calculation at the time that don't get writing back to X, our just simple: case X

    case X+Y
    3:do that
    4:or this
    5:maybe this
    other: do this for everything else
  • ChrisGaddChrisGadd Posts: 310
    edited 2015-02-19 06:58
    The case expression can only be a single value, not a range.
    You can test ranges in the match expressions however, so you could do:
      case x
        1..3,5..10 : dira[4]~
        1..4,6..10 : dira[5]~
        1..5,7..10 : dira[6]~
    
    However, that probably doesn't do what you were expecting either, as case stops at the first match. If x were 7 for instance, only pin 4 would be set to an input.
    You could work around that by creating something like:
      case x
        1..3,7..10    : dira[4..6]~
        1..3,6..10    : dira[4..5]~
        1..3,5,7..10 :
          dira[4]~
          dira[6]~
        1..4,7..10    : dira[5..6]~
    
    or use if / thens as in:
      if x <> 4
        dira[4]~
      if x <> 5
        dira[5]~
      if x <> 6
        dira[6]~
    
    Or simply set every pin other than x in a range to an input, and leave x unchanged: dira := |< x & dira | dira &! %01110000
  • edited 2015-02-19 12:24
    mikea wrote: »
    Hi, I'm trying to use case to test 3 values for not equal to x. If 4 is not equal to x, then dira[4]~ and so on. The compiler wants to see an expression term and highlights "<>". I'm not clear on what an expression term is. Thanks

    case  <> x
       4: dira[4]~
       5: dira[5]~
       6: dira[6]~
    

    The case statement requires an 'expression term' it can evaluate to a single value.
    a := 4
    case a
      1..4 : b := 5
         5 : b := 6
         6 : b := 7
    

    If there's only three values you could use a series of if statements
    value1 := 1
    value2 := 2
    value3 := 3
    x := 2
    if value1 <> x
       dira[ value ]~
    if value2 <> x
       dira[ value2 ]~
    if value3 <> x
       dira[ value3 ]~
    

    Sandy
Sign In or Register to comment.