0

When for statement is executed the value of counter variable has to be increased by one because I use pre-increment operator.

#include <stdio.h>
int main (void)
{
    unsigned int counter ;
    for ( counter = 1; counter <= 10; ++counter /*here is problem*/) {
        printf( "%u\n", counter );
    }
}

Problem -

When the program is executed, the value of counter variable initially is 1 instead of 2.

user3335966
  • 2,673
  • 4
  • 30
  • 33
  • 1
    It makes no difference here whether you pre- or post-increment. It does not happen until the end of the first loop. Since there is no side-effect, `++counter` is the same as `counter++`. – Weather Vane Jun 20 '16 at 23:48
  • `for ( counter = 1; counter++ <= 10;)` – EOF Jun 20 '16 at 23:52
  • Why do you think it should be 2? – user253751 Jun 21 '16 at 00:20
  • The increment code is executed *after* each loop iteration. I.e., *after* the `printf`. Since you're not using the result of your increment expression, it makes no difference whether you use a pre- or post-increment operator. That only affects the value of the expression, e.g. `x = ++counter;` vs. `x = counter++;` – Tom Karzes Jun 21 '16 at 00:21
  • 1
    Why is this C question marked as a duplicate of a Java question? – Pang Jun 21 '16 at 03:38
  • @Pang +1 You do have a point there. – Mestica Jun 21 '16 at 04:31
  • @Pang: because the linked question is really language-agnostic. The answers apply entirely. – Nick Matteo Jun 21 '16 at 15:13

3 Answers3

1

In a for loop

for(first statement; second statement; third statement){//...}; 

the third statement which is generally used for updation, is executed at the end of each iteration, therefore your variable counter would be 1 during the first iteration and becomes 2 at the end of first iteration.


If you want to make your counter variable to be incremented at the start of iteration, then try using it ++counter in the second statement of for loop this way :

for ( counter = 1; ++counter <= 10;)

Reason :

because, a for loop is a pre-test loop and condition which is generally the second statement is checked at the start of each iteration. So now your counter is incremented at the start of each iteration

Cherubim
  • 5,287
  • 3
  • 20
  • 37
0

When for statement is executed then

at first time for loop's third statement is not checked weather you use an increment operator or condition that is given to the for loop but

when loop start to iterate then if you use any increment or decrements operator at third statement of for loop then it works and if you use any condition at third statement of for loop then your program will never end causing an infinite loop.

mdadil2019
  • 807
  • 3
  • 12
  • 29
0

If you want the count to start from 2, you should initialize count in the for loop as

for ( counter = 2; counter <= 10; counter++) {
    printf( "%u\n", counter );
}

C follows the following syntax for for loops:

for ( init; condition; increment ) {
   statement(s);
}

The init part here is where you make your initializations.

Mestica
  • 1,489
  • 4
  • 23
  • 33