Difference between pre-increment and post-increment in a loop?

后端 未结 22 2119
暗喜
暗喜 2020-11-21 23:41

Is there a difference in ++i and i++ in a for loop? Is it simply a syntax thing?

22条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 00:26

    One (++i) is preincrement, one (i++) is postincrement. The difference is in what value is immediately returned from the expression.

    // Psuedocode
    int i = 0;
    print i++; // Prints 0
    print i; // Prints 1
    int j = 0;
    print ++j; // Prints 1
    print j; // Prints 1
    

    Edit: Woops, entirely ignored the loop side of things. There's no actual difference in for loops when it's the 'step' portion (for(...; ...; )), but it can come into play in other cases.

提交回复
热议问题