Shop OBEX P1 Docs P2 Docs Learn Events
Code question - Use of // — Parallax Forums

Code question - Use of //

MichelBMichelB Posts: 154
edited 2009-10-17 13:53 in BASIC Stamp
Hi specialists of code. In NewDistanceFollowingWithAverageAndDeadband.bs2 (Andy Lindsay) we can find:
...
Elements·· CON·· 4
...
'
{ Subroutine - Get IR Distances }

Get_IR_Distances:

index = index + 1
index = index // Elements (or index = index // 4)

If it was a beginner writting the 2 last lines how it should be?
Something with IF... THEN, ENDIF, but what?
Thank you.
Best regards

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-17 13:04
    The "//" operator gives you the remainder from a division. In the case of "index = index // Elements", you divide "index" by "Elements" and use the remainder as the result. If "Elements" is 4 like in the comment to the right ("or index = index // 4"), you'd get the following for results:
    index     index//4
       0           0
       1           1
       2           2
       3           3
       4           0
    


    This has the effect of index "wrapping around" at Elements. It's not exactly the same, but these two statements could be rewritten as:
    index =  index + 1
    if index = Elements then index = 0
    
  • MichelBMichelB Posts: 154
    edited 2009-10-17 13:53
    OK Mike, well understood, thank you.
Sign In or Register to comment.