rpj
rpj

Reputation: 93

Output of the for loop?

int main() {
    int i,j,count;
    count=0;
    for(i=0; i<5; i++);
    { 
        for(j=0;j<5;j++);
        {
            count++;
        }
    }
    printf("%d",count);
    return 0;
}

here if we put ; after for() then for loop doesn't run anything. Then how does count becomes 1 after execution of the program?

Upvotes: 2

Views: 1557

Answers (4)

Sanat005
Sanat005

Reputation: 33

Your code is look like below,after putting an ; at the end of both for loop.

int main() {
    int i,j,count;
    count=0;
    for(i=0; i<5; i++);
    {
          //do nothing
    }
    for(j=0;j<5;j++);
    {
         //do nothing
    }
    count++;
    printf("%d",count);
    return 0;
}

both for loop do nothing and after that only one statement that is count++, so it is increment the value of count.That is why it print the value in Output:1.

Upvotes: 0

lenik
lenik

Reputation: 23556

Once you ignore the for loops, the rest becomes:

int main() {
    int i,j,count;
    count=0;
    { 
        {
            count++;
        }
    }
    printf("%d",count);
    return 0;
}

So you may see the counter is getting incremented once.

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

There is one occurrence of

 count++;

in the program which increments the counter only once.

To explain:

int main() {
      int i,j,count;
      count=0;
      for(i=0; i<5; i++);  
     {                       // i == 5, count == 0
        for(j=0;j<5;j++);    
            {                // i == 5, j == 5, count == 0
                count++;     // i == 5, j == 5, count == 1
            }
      }
      printf("%d",count);     //i == 5, j == 5, count == 1
      return 0;
     }

That said, as you mentioned

here if we put ; after for() then for loop doesn't run anything.

is not entirely true. If you put the ; after the loop construct, it behaves as if the loop body is empty, i.e., there is no code in the loop body. The loop still runs, and the next block is not considered as the loop body, rather part of the unconditional flow.

Don't be fooled just by the indentation. Your code, can be re-written as

int main(void) {
      int i,j,count;
      count=0;
      for(i=0; i<5; i++)   // ; removed
      {
         // no code
      }
      {                         // just another block, not previous loop body 
      for(j=0;j<5;j++)    // ; removed
      {
         // again no code
      }
      {                         // again just another block, not previous loop body 
          count++;
      }
      }
      printf("%d",count);
      return 0;
     }

Which basically boils down to:

int main() {
      int count = 0;

      {
         count++;
      }

      printf("%d",count);
      return 0;
     }

Upvotes: 6

riley lyman
riley lyman

Reputation: 327

I think your confusion might be about putting the ; after the for statment.

for (i = 0; i < 5; i++);

is the same as writing

for (i = 0; i < 5; i++) {
;
}

C looks at the first statement after the for (...) (in this case that is the ; statement), and then executes that as the body of the for-loop. So, when you write

for (i = 0; i < 5; i++);
{
 // other stuff
}

Your program basically skips the for-loop, since it does ; 5 times. Then, the //other stuff gets executed once, since it's not a part of the for-loop. Does this make sense?

Therefore, your code only increments count once.

Upvotes: 2

Related Questions