Shop OBEX P1 Docs P2 Docs Learn Events
Increment a variable — Parallax Forums

Increment a variable

kingbpkingbp Posts: 22
edited 2011-12-10 01:52 in General Discussion
Hello Is thee anyone out there that can give me an example on how to increment a variable in PBASIc, I tried doing this Turns + 1, to increment a variable i called turns but it didn't work does anyone know how to increment a variable the proper way?

Comments

  • FranklinFranklin Posts: 4,747
    edited 2011-12-09 15:45
    You should be reading the help files. there are manuals that have the syntax and conventions for programming that you will need to do any meaningful work. turns = turns+1
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2011-12-09 18:32
    EDIT: I was wrong, my mind must have been in SPIN mode..... :)
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-12-09 18:44
    First, "turns" has to be declared as a variable, here it is a byte:
    turns var byte

    You can initialise it to be 0, 1 or any other value. Here, it starts at 1:
    turns = 1

    Here's how you increment:
    turns = turns + 1

    Here's how you decrement:
    turns = turns - 1

    [ "Turns++" is not PBasic]
  • John BoardJohn Board Posts: 371
    edited 2011-12-10 01:52
    Just confirming,

    The common way is either (but not in this case) turns++. This works in many languages, such as SPIN (I think), Java, C++ (I think) etc.

    The next best thing is:

    turns = turns + 1
    turns = turns - 1

    Bear in mind that Micros and other computery devices start counting from 0.

    -John
Sign In or Register to comment.