0

I am reviewing a code with the following snippet for minimal reproducible example. So here the code runs an iteration to initialize an array done as follows:

#include <stdio.h>
int i,j;
N=10;
int done[500];
void main() {

 done[0]=done[N]=1;
 for(i=0;i<N+1;done[i++]=0){
  printf("Done val %d iteration %d\n",done[i],i);

 }
}

The thing I am concerned with that I purpossely initialized the values of done[0]=done[1]=1 However when I run the initialization loop, the values at index 0 and 10 remains unchanged. I would like to understand how is the syntax of done[i++] actually evaluated?

8
  • Did you mean to say "index 0 and 10"? Commented Jun 11, 2020 at 21:28
  • yes, is it wrong? Commented Jun 11, 2020 at 22:24
  • Yes. You did N=10 and done[N]=1. Commented Jun 12, 2020 at 1:10
  • I quite didnt catch what you are pointing out. What I meant is done[10] = done[0]=1 so N=10 Commented Jun 12, 2020 at 13:37
  • 1
    Then why did you write "the values at index 0 and 1 remains unchanged". Didn't you mean "the values at index 0 and 10", since those are the ones that you initialized differently? Commented Jun 12, 2020 at 14:22

2 Answers 2

1

The increment step of a for loop takes place after the loop body is executed. It's equivalent to the following code:

i = 0;
while (i < N+1) {
    printf("Done val %d iteration %d\n",done[i],i);
    done[i++] = 0;
}

As you can see, it's printing the value of done[i] before it changes it. So it prints the original value.

Sign up to request clarification or add additional context in comments.

1 Comment

i see makes sense
1

The code snippet you provided will never compile correctly to begin with! Although, I am going to assume you already know the reasons why. Therefore, I will simply answer your question.

Firstly, you did not initialize your indexes the way you expected. You did the following instead:

done[0]=done[N]=1;

Thus, index zero will be set to one, yet index ten is set to one instead of index one. Therefore, you should have done the following:

done[0]=done[1]=1;

As for the for loop itself, you must understand that your increment variable is executed last! A for loop is a three step process that follows a procedural order where,

  1. increment variable gets initialized
  2. Conditional statement gets checked
  3. If conditional statement is true, then execute code body then increment/decrement the increment variable, then go to step two. Otherwise, exit the for loop!

Therefore, done[i++]=0 gets evaluated after your printf() statement in the provided code block! For more information please view C++ for loop - Tutorialspoint

3 Comments

Whoops, Barmar already gave a good answer, thus, my answer is redundant! I guess I spent too much time looking for good reference. Sorry about that!
Welcome and +1 for good detailed answer. The code does compile and execute fine in my machine. I really like the procedural order that you described and for your effort in detailed answer. Remember that overtime others vote as well so no matter if your answer is redundant or providing additional perspective, the upvotes does get built up over time.
Oh okay, I learned something new! Thank you very much for your feedback!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.