Conditions used in if statements changes values of the variables?
3dogpottery
Posts: 78
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
Less-than-or-equal in Spin is =<
The operators you've used (>= and <=) are assignments and do change the value of the variable.
-Phil
If you mean greater than or equal it's =>
If you mean less than or equal it's =<
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:
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.
Oh and that "Heater". As in radio tubes.