-2

in the following loop which i++ will be executed first?the one which is inside the for loop or the one at line no 3?

enter code here

1.for(i = 0; i < 3; i++) {
2.a[i] = a[i] + 1;
3.i++;
4.}
  • 2
    Questions about solving homework must demonstrate effort, see [help/on-topic]. – user202729 Aug 09 '18 at 14:22
  • Trick question, the first time through the loop its the inside, all other times it is in the FOR declaration, you really don't have a i++ "outside of the loop", Declaration, cond check, skips the iteration. then the inside scope runs, then i is incremented again. So you get one increment first time entering the loop, for every time after that it is just like having i++; twice in a row inside the scope. You can switch that up with ++i . . . . – Wookies-Will-Code Aug 09 '18 at 14:23

2 Answers2

1

The one inside the loop is executed first. The one in the loop declaration is always executed at the end of each loop before it starts its next iteration.

JamesHoux
  • 2,999
  • 3
  • 32
  • 50
0

Is it difficult to test it yourself?:

#include <stdio.h>

int main(void) {
    for(int i = 0; i < 50; i++) 
    {
        printf("i before increment = %d\n", i);
        i++;
        printf("i after increment = %d\n", i);
    }
    return 0;
}

Run and test it yourself https://ideone.com/N76Q2n

and everything will be clear.

0___________
  • 60,014
  • 4
  • 34
  • 74