Shop OBEX P1 Docs P2 Docs Learn Events
First code issues — Parallax Forums

First code issues

dieseldave1984dieseldave1984 Posts: 2
edited 2013-02-20 19:38 in Propeller 1
Hello all, I am just now working on my first program and cannot figure out this part of code below in red, also if anyone has any tips to clean it up some. I will have several of these full statements listed below on a repeat loop checking the first line "if(psi<,>,== a number) " it will run the code below it and if not will check the next "if(psi<,>,== a number) in the loop to see if its true/false and run the secondary code. Any help is greatly appreciated, thanks!

if(psi < 100)
if (position < 400)
direction :=1
pst.str(string("set1"))
pst.newline
elseif (position > 500)
direction :=2
pst.str(string("set2"))
pst.newline
if (position == (401..499)) <==== I need the code to check if its between 401-499 and every number in between those (if thats even posible)
direction :=3
pst.str(string("set3"))
pst.newline

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-02-20 19:08
    This is one way:
    if position => 401 and position =< 499
    

    Make sure and keep the "=" and "<>" signs in the correct order. "=<" and "<=" are not the same.

    Another is with the case statement.
    case position
        0..400:
          direction := 1
          pst.str(string("set1"))
        401..499:
          direction := 2
          pst.str(string("set2"))
        other:
          direction := 3
          pst.str(string("set3"))
        pst.newline
    

    Since indentation is part to the syntax in Spin, it's important to use code tags when posting code.

    Phil made a tutorial on how to post code. Here's a link.

    attachment.php?attachmentid=78421&d=1297987572
  • dieseldave1984dieseldave1984 Posts: 2
    edited 2013-02-20 19:24
    Thanks alot! I will add it in my code tomorrow and see how it works.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-02-20 19:38
    I should have given a better explanation of the "if/elseif" option.

    By starting at one end of the possible values and working towards the other, you can eliminate the need of using "and" as the example in post #2.

    For example, by starting at the smallest group you want to test against and working your way up, you don't need to exclude the lower range since if the value had been lower, the code would found a match and stopped checking the higher values.

    Of course it's important to use "elseif" and not just a series of "if" statements.
    if position =< 400
        direction := 1
        pst.str(string("set1"))
      elseif position =< 499  
        direction := 2             ' this code is executed for all values of position between 401 and 499
        pst.str(string("set2"))
      else
        direction := 3             ' this code is executed for all values of position 500 and greater
        pst.str(string("set3"))
      pst.newline
    
Sign In or Register to comment.