If
g3cwi
Posts: 262
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
Beyond the space and time penalties Kuroneko mentioned, note that the two versions are not equivalent.
Wont 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.
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:
Generates 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: Andy
Cheers
Richard