[var++] or [++var] for [for] loop

I just recently noticed that for [for] loop in every single code I wrote and from code snippet in VC#, both use the same following code pattern (notice the increment pattern):

for (int i = 0; i < length; i++) {

}

AFAIK, the increment condition I should be the next Idea value which is [i = i + 1]. And I think [i = i + 1] is better translated as [++i] rather than [i++], though both of them behave indifferently in the looping process.

What do you think about this, guys




Answer this question

[var++] or [++var] for [for] loop

  • MaNicXs

    I think the same.

    But on the same topic, what's the difference from the efficiency point of view between

    for (i=0; i<max; i++) makeSomething();

    and

    for (i=0; i++<max;) makeSomething();

    or even (if correct)

    for (i=0; i++<max; makeSomething());


  • Richard Pfeiffer

    its the same thing really, no difference just user perference i believe:

    i = i + 1

    i++

    ++i

    to me I believe is the same thing



  • jhudson

    There's loads of games you can play with for(), especially since an expression can also be a statement. The equivalent code for
    for (initstatement; testexpr; loopstatement) statementblock
    is:
    initstatement;
    loop:
    if (testexpr) {
    statementblock;
    loopstatement;
    goto loop;
    }

    Of course, there's only one canonical form that is considered highly readable...



  • [var++] or [++var] for [for] loop