If
I have been attempting to understand other people's SPIN code. One thing that I see often and puzzles me is a use of "If";
If VAR
Do something
This must be an implied test of some sort? I am happy with:
If VAR>100
Do something
What does the first example mean?
Richard
If VAR
Do something
This must be an implied test of some sort? I am happy with:
If VAR>100
Do something
What does the first example mean?
Richard

Comments
Richard
I'm thinking that if there is no penalty, I will try to use the second one as I think it does read more logically.
Depends on spins implementation. Try two timing loops with both methods and see which one is faster using the counter or other timing method.
Let us know what you find
if enabled ' do somethingBeyond the space and time penalties Kuroneko mentioned, note that the two versions are not equivalent.
if var == true 'do something if var equals -1Wont this condition only be met if var equals -1 aka "TRUE"?
Addit : re Duane, do you mean that 'if var' is not the same as 'if var == true' ?
If so then I'm going to be asking the same question as our OP in post #1
Thanks for pointing that one out. Kinda negates the main question here as we are no longer apples to apples, rather apples with significant side effects.
Yes.
Just to try to get this straight in my brain, I wrote this program.
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 _DebugBaud = 57600 OBJ debug : "FullDuplexSerial" PUB Main Debug.Start(31, 30, 0, _DebugBaud) waitcnt(clkfreq * 3 + cnt) TestConditions(-1) TestConditions(true) TestConditions(0) TestConditions(false) TestConditions(1) TestConditions(2) TestConditions(-10) repeat PRI TestConditions(var1) Debug.str(string(13, 13, "var1 = ")) Debug.dec(var1) if var1 Debug.str(string(13, "(if var1) condition met.")) else Debug.str(string(13, "(if var1) condition not met.")) if var1 <> false Debug.str(string(13, "(if var1 <> false) condition met")) else Debug.str(string(13, "(if var1 <> false) condition not met.")) if var1 == true Debug.str(string(13, "(if var1 == true) condition met")) else Debug.str(string(13, "(if var1 == true) condition not met.")) if var1 == false Debug.str(string(13, "(if var1 == false) condition met")) else Debug.str(string(13, "(if var1 == false) condition not met.")) ifnot var1 Debug.str(string(13, "(ifnot var1) condition met")) else Debug.str(string(13, "(ifnot var1) condition not met.")) ifnot var1 <> false Debug.str(string(13, "(ifnot var1 <> false) condition met")) else Debug.str(string(13, "(ifnot var1 <> false) condition not met.")) ifnot var1 == true Debug.str(string(13, "(ifnot var1 == true) condition met")) else Debug.str(string(13, "(ifnot var1 == true) condition not met.")) ifnot var1 == false Debug.str(string(13, "(ifnot var1 == false) condition met")) else Debug.str(string(13, "(ifnot var1 == false) condition not met."))This is the output.
You can see that "true" is treated the same as "-1" and "false" is treated the same as "0".
I am wondering about using this in my code - saves space by adding the ' as a comment and it is explicit about what the test condition is.
Kye uses all sort of tricks with "if var" and "ifnot var". I often get myself into trouble when I try to do the same.
One other thing to be aware of is "true" is a long so bytes and words wont ever be true.
For example, this code:
VAR long varLong byte varByte OBJ debug : "FullDuplexSerial" PUB Main Debug.Start(31, 30, 0, _DebugBaud) waitcnt(clkfreq * 3 + cnt) [B] varLong := true varByte := true [/B] if varLong == true Debug.str(string(13, "(if varLong == true) condition met.")) else Debug.str(string(13, "(if varLong == true) condition not met.")) if varByte == true Debug.str(string(13, "(if varByte == true) condition met.")) else Debug.str(string(13, "(if varByte == true) condition not met.")) repeatGenerates this output:
does it mean 'if not not equal true" or is it "if false".
It seems the opposite of Mike Green's explanation on post #12 but in what way is it opposite? I think I can understand it if one sticks to "true" and "false" but not when you add numbers. Definitely would need a long comment after such a statement. My brain hurts!
Mine too, whenever I try to make sense of "ifnot" statements.
Many languages have an "unless" keyword, "ifnot" or "if not" is an awkward construction.
This form of IF is perfect for variables which are used for flags or have just two states like set or not set:
if enabled 'do this ifnot enabled 'do that buttonPressed := ina[0] if buttonPressed 'do something ifnot buttonPressed 'do another thing if pinHigh '... ifnot pinHigh '...AndyCheers
Richard