• 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.

    • marcos@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      6 months ago

      That *++ operator from C is indeed confusing.

      Reminds me of the goes-to operator: --> that you can use as:

      while(i --> 0) {
      
      • letsgo@lemm.ee
        link
        fedilink
        arrow-up
        1
        ·
        6 months ago

        That’s not a real operator. You’ve put a space in “i–” and removed the space in “-- >”. The statement is “while i-- is greater than zero”. Inventing an unnecessary “goes to” operator just confuses beginners and adds something else to think about while debugging.

        And yes I have seen beginners try to use <-- and --<. Just stop it.

        • marcos@lemmy.world
          link
          fedilink
          arrow-up
          0
          arrow-down
          1
          ·
          6 months ago

          The sheer number of people that do not expect a joke on this community… (Really, if you are trying to learn how to program pay attention to the one without the Humor on the name, not here.)

          Well, I guess nobody expects.