Shop OBEX P1 Docs P2 Docs Learn Events
If — Parallax Forums

If

g3cwig3cwi Posts: 262
edited 2012-04-24 08:36 in Propeller 1
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

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-04-23 11:08
    I think "if var" is the same as "if var <> 0".
  • pedwardpedward Posts: 1,642
    edited 2012-04-23 11:11
    Like Duane said, non-zero value. If the variable is unset, the test fails, if it's set to anything, it passes.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-23 11:15
    The Propeller Manual defines FALSE as zero and TRUE as all one bits. The various Boolean operators (NOT, AND, OR) and the comparison operators (<, ==, >, <>, =>, =<) produce FALSE and TRUE results. The IF statement and the REPEAT UNTIL and REPEAT WHILE statements accept any non-zero value as TRUE.
  • g3cwig3cwi Posts: 262
    edited 2012-04-23 11:35
    Thanks - all clear now!

    Richard
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-04-23 17:46
    A question for the experts - is there any penalty in terms of extra code produced by the compiler with
    if var
    
    vs
    if var == true
    

    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.
  • frank freedmanfrank freedman Posts: 1,983
    edited 2012-04-23 17:51
    Dr_Acula wrote: »
    A question for the experts - is there any penalty in terms of extra code produced by the compiler with
    if var
    
    vs
    if var == true
    

    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 :)
  • kuronekokuroneko Posts: 3,623
    edited 2012-04-23 17:53
    @Dr: The latter is bigger and slower. Your choice. That said, if you name the variables properly then there is nothing wrong with the first version:
    if enabled
        ' do something
    
  • mparkmpark Posts: 1,305
    edited 2012-04-23 18:26
    Dr_Acula wrote: »
    A question for the experts - is there any penalty in terms of extra code produced by the compiler with
    if var
    
    vs
    if var == true
    

    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.

    Beyond the space and time penalties Kuroneko mentioned, note that the two versions are not equivalent.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-04-23 18:32
    if var == true
        'do something if var equals -1
    


    Wont this condition only be met if var equals -1 aka "TRUE"?
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-04-23 18:39
    Thanks kuroneko. Well, if there is a cost in terms of speed and/or code space, that would explain why many people use the shorter version. So maybe the answer might be just to put a comment there eg
    if var    '  == 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
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-23 19:07
    "if var" is not the same as "if var == true" because the latter requires that the value of var be exactly -1 while the former only requires that var be non-zero. In most cases, the former is perfectly acceptable.
  • frank freedmanfrank freedman Posts: 1,983
    edited 2012-04-23 19:15
    Mike Green wrote: »
    "if var" is not the same as "if var == true" because the latter requires that the value of var be exactly -1 while the former only requires that var be non-zero. In most cases, the former is perfectly acceptable.

    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.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-04-23 19:30
    So is the equivalent code "if var" the same as ?
    if var <> false
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-04-23 19:35
    Dr_Acula wrote: »
    So is the equivalent code "if var" the same as ?
    if var <> false
    

    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.
    var1 = -1
    (if var1) condition met.
    (if var1 <> false) condition met
    (if var1 == true) condition met
    (if var1 == false) condition not met.
    (ifnot var1) condition not met.
    (ifnot var1 <> false) condition not met.
    (ifnot var1 == true) condition not met.
    (ifnot var1 == false) condition met
    var1 = -1
    (if var1) condition met.
    (if var1 <> false) condition met
    (if var1 == true) condition met
    (if var1 == false) condition not met.
    (ifnot var1) condition not met.
    (ifnot var1 <> false) condition not met.
    (ifnot var1 == true) condition not met.
    (ifnot var1 == false) condition met
    var1 = 0
    (if var1) condition not met.
    (if var1 <> false) condition not met.
    (if var1 == true) condition not met.
    (if var1 == false) condition met
    (ifnot var1) condition met
    (ifnot var1 <> false) condition met
    (ifnot var1 == true) condition met
    (ifnot var1 == false) condition not met.
    var1 = 0
    (if var1) condition not met.
    (if var1 <> false) condition not met.
    (if var1 == true) condition not met.
    (if var1 == false) condition met
    (ifnot var1) condition met
    (ifnot var1 <> false) condition met
    (ifnot var1 == true) condition met
    (ifnot var1 == false) condition not met.
    var1 = 1
    (if var1) condition met.
    (if var1 <> false) condition met
    (if var1 == true) condition not met.
    (if var1 == false) condition not met.
    (ifnot var1) condition not met.
    (ifnot var1 <> false) condition not met.
    (ifnot var1 == true) condition met
    (ifnot var1 == false) condition met
    var1 = 2
    (if var1) condition met.
    (if var1 <> false) condition met
    (if var1 == true) condition not met.
    (if var1 == false) condition not met.
    (ifnot var1) condition not met.
    (ifnot var1 <> false) condition not met.
    (ifnot var1 == true) condition met
    (ifnot var1 == false) condition met
    var1 = -10
    (if var1) condition met.
    (if var1 <> false) condition met
    (if var1 == true) condition not met.
    (if var1 == false) condition not met.
    (ifnot var1) condition not met.
    (ifnot var1 <> false) condition not met.
    (ifnot var1 == true) condition met
    (ifnot var1 == false) condition met
    

    You can see that "true" is treated the same as "-1" and "false" is treated the same as "0".
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-04-23 19:49
    Ah, so the question by g3cwi is more intriguing than it first appears! You save space leaving out the condition, but the condition is not a "true", it is a "not false"

    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.
    if var ' <> false
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-04-23 20:06
    I've started using "if var" conditions only recently.

    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.")) 
      repeat
    

    Generates this output:
    (if varLong == true) condition met.
    (if varByte == true) condition not met.
    
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-04-23 21:09
    yikes, what does " "ifnot var" mean?

    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!
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-04-23 21:12
    Dr_Acula wrote: »
    My brain hurts!

    Mine too, whenever I try to make sense of "ifnot" statements.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-23 21:43
    "ifnot var" is the same as "if not var", just more efficient, so it means "if var == 0"
  • Mark_TMark_T Posts: 1,981
    edited 2012-04-24 04:56
    Mike Green wrote: »
    "ifnot var" is the same as "if not var", just more efficient, so it means "if var == 0"

    Many languages have an "unless" keyword, "ifnot" or "if not" is an awkward construction.
  • AribaAriba Posts: 2,690
    edited 2012-04-24 05:44
    As Kuroneko said, it depends a lot on the variable name if it is readable or not.
    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
        '...
    
    Andy
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2012-04-24 06:10
    IF you can keep your head when all about you 
    Are losing theirs and blaming it on you,
    If you can trust yourself when all men doubt you,
    But make allowance for their doubting too;
    If you can wait and not be tired by waiting,
    Or being lied about, don't deal in lies,
    Or being hated, don't give way to hating,
    And yet don't look too good, nor talk too wise:
    If you can dream - and not make dreams your master;
    If you can think - and not make thoughts your aim;
    If you can meet with Triumph and Disaster
    And treat those two impostors just the same;
    If you can bear to hear the truth you've spoken
    Twisted by knaves to make a trap for fools,
    Or watch the things you gave your life to, broken,
    And stoop and build 'em up with worn-out tools:
    
    If you can make one heap of all your winnings 
    And risk it on one turn of pitch-and-toss,
    And lose, and start again at your beginnings
    And never breathe a word about your loss;
    If you can force your heart and nerve and sinew
    To serve your turn long after they are gone,
    And so hold on when there is nothing in you
    Except the Will which says to them: 'Hold on!'
    
    If you can talk with crowds and keep your virtue,
    ' Or walk with Kings - nor lose the common touch,
    if neither foes nor loving friends can hurt you,
    If all men count with you, but none too much;
    If you can fill the unforgiving minute
    With sixty seconds' worth of distance run,
    Yours is the Earth and everything that's in it,
    And - which is more - you'll be a Man, my son!
    
  • g3cwig3cwi Posts: 262
    edited 2012-04-24 08:36
    Oh dear. I have just read the whole thread and feel slightly sick. The manual seems rather superficial in some respects. I wish it had more explicit examples.

    Cheers

    Richard
Sign In or Register to comment.