Shop OBEX P1 Docs P2 Docs Learn Events
Conditions used in if statements changes values of the variables? — Parallax Forums

Conditions used in if statements changes values of the variables?

3dogpottery3dogpottery Posts: 78
edited 2014-02-03 13:50 in Propeller 1
I am using four if statements to compare two variables. After the statements are implemented, the values of the two variables used in the statements are changed. Additionally, the results of the if statements are erroneous. Can't figure out why.
CON
    _clkmode = xtal1 + pll16x                           
    _xinfreq = 5_000_000
OBJ
Ser     :       "FullDuplexSerial"               
           
VAR
        long Value, a, b 
        
PUB Main_Routine
''Serial communication Setup
      Ser.start(31, 30, 0, 38_400)
 repeat
      a := 1
      b := -1
      Ser.tx(16)    'Clear Screen
      Ser.tx(1)     'Home Position
      Ser.str(string("Before Comparison... "))  
      Ser.tx(13)
      Ser.str(string("a = "))   
      Ser.dec(a)
      Ser.tx(13)    
      Ser.str(string("b = "))   
      Ser.dec(b)
      Ser.tx(13)
      Ser.tx(13)
 
      if (a >= 0) AND (b > 0)
             Value := 1
      if (a > 0) AND (b <= 0)
             Value := 2
      if (a <= 0) AND (b < 0)
             Value := 3
      if (a < 0) AND (b >= 0)
             Value := 4             
      Ser.str(string("After the group of 'if' comparisons, not only have the values"))  
      Ser.tx(13)
      Ser.str(string("of a and b been changed, the result is wrong."))
      Ser.tx(13)
      Ser.tx(13)
           
      Ser.str(string("a = "))   
      Ser.dec(a)
      Ser.tx(13)    
      Ser.str(string("b = "))   
      Ser.dec(b)
                 
      Ser.tx(13)
      Ser.str(string("Value = "))     
      Ser.dec(Value)
      waitcnt(clkfreq + cnt)

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-02-03 12:04
    Greater-than-or-equal in Spin is =>
    Less-than-or-equal in Spin is =<

    The operators you've used (>= and <=) are assignments and do change the value of the variable.

    -Phil
  • Heater.Heater. Posts: 21,230
    edited 2014-02-03 12:09
    I think you gave the wrong >= and >= operators.

    If you mean greater than or equal it's =>
    If you mean less than or equal it's =<
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-03 12:18
    (Phil and Heater answered while I was playing with your code)

    Very subtle error: in Spin, any operator that ends with = will do an assignment.

    In most languages we use >= for greater than or equal; in Spin we must use =>. Same for =<.

    Try this:
    if ((a => 0) and (b > 0))
          value := 1
        elseif ((a > 0) and (b =< 0))
          value := 2
        elseif ((a => 0) and (b < 0))
          value := 3
        elseif ((a < 0) and (b =< 0))
          value := 4
    


    Note that you have a possible issue in your testing. Use 1 for a and -1 for b, the second and third tests can evaluate as true. Your original code gives 3 because the second of the two if tests updates the value. In my version, only the first success works; other tests are ignored. You need to reconcile the input/output behavior.
  • 3dogpottery3dogpottery Posts: 78
    edited 2014-02-03 13:33
    Thanks Phil Pilgrim, Heather, and JohnnyMac. I never realized there was a difference. Thank you very much for taking the time to help me out. I usually program in C, and always have a problem when I go back to Spin. incidentally, I am retired, and am playing around with programming the Parallax Propeller just to keep my brain from totally rotting away. I am trying to make a ballbot. The if statements are used to figure out which quadrant the robot is tilting in on an XY plane. Anyway, thanks again!
  • Heater.Heater. Posts: 21,230
    edited 2014-02-03 13:50
    Yep, Catches out us old C hands every time. It's cruel trick Chip played on us:)

    Oh and that "Heater". As in radio tubes.
Sign In or Register to comment.