• fluckx@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    6 months ago
    p = 1
    
    x = ++p
    // x = 2
    // p = 2
    
    p = 1
    x  = p++
    // x = 1
    // p = 2
    

    ++p will increase the value and return the new value

    p++ will increase the value and return the old value

    I think p = p + 1 is the same as p++ and not as ++p. No?

      • fluckx@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        6 months ago

        Yes.

        p++ == p+= 1 == p = p + 1 are all the same if you use it in an assignment.

        ++p is different if you use it in an assignment. If it’s in its own line it won’t make much difference.

        That’s the point I was trying to make.

        • SpaceNoodle@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          6 months ago

          No.

          ++p returns incremented p.

          p += 1 returns incremented p.

          p = p + 1 returns incremented p.

          p++ returns p before it is incremented.