0

Sorry to completely rewrite this post, but I was way off on my troubleshooting. Hopefully this prevents someone's headache in the future...

Now, for example code.

    function compare(uint8 a, uint8 b)
    private
    requireUnpaused
    returns(bool)
    {
        // increment a by 1
        a = a++;
        // if a + 1 = 3, then loop it around the cycle to be 0
        if (a >= 3) {
            a = 0;
        }
        // compare a to b.  If a = b, a is the winner - return true
        return a == b ? true : false;
    }

This code works as expected if I replace "a = a++" with "a = ++a", just "a++" or "++a", or even "a = a + 1".

This drove me crazy for a few days. My question for someone more versed in this than I: Why does "a = a++" not work, when every other way of incrementing "a" by 1 does?

bcral
  • 31
  • 1
  • 3
  • Have you try to debug it in remix? I'm still new too, so I don't really get what your code is doing since I learn Solidity 0.8.0 that some code looks new to me :) One thing that I learn is that to give parameter name `_` prefix do it doesn't mix with your global. – Abraham Anak Agung Jun 09 '21 at 07:21
  • Thanks for the input. I'm zeroing in on the solution, and when I find it I'll rename, rewrite, and answer this as appropriate to whatever the problem ends up being. – bcral Jun 09 '21 at 15:08
  • To answer you post increment and pre increment: TLDR ``` uint a = 0; a = a++; // a = 0 because the old value will return a = ++a; // a = 1 the new value will return which is 0 + 1 ``` more here: https://stackoverflow.com/questions/484462/difference-between-pre-increment-and-post-increment-in-a-loop – Abraham Anak Agung Jun 10 '21 at 02:13

0 Answers0