aviabhinn98's blog

By aviabhinn98, history, 4 years ago, In English

for(int i=0;i<n;i++) { }

vs

for(int i=0;i<n;++i){ }

Both these loop run n times but we know that ++i and i++ are different. So can anyone explain what is happening in backend in gcc compiler using cpp? Thanks in advance

  • Vote: I like it
  • -31
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Why do you expect them to have different runtimes?

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

++i and i++ only differ if you actually use their values, something like j = ++i (which is equivalent to j = i+1; i++) vs. j = i++ (which is equivalent to j = i; i++). Other than that they do exactly the same thing

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Basically, because the condition of stopping in your loop is checked before the increment of i, and after the increment of i (either i++ or ++I), I increases by 1.