0

I have a confusion in basic concept for C language for loop increment. This is my code:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int j=0;int a;
    for(j;j<=3;++j)
    {
        printf("hello\n");
        a=j;
    }
    printf("%d,%d\n",j,a);
}

My question is: Why is the output of a not equal to output of j? I was thinking that if j is getting increased for every iteration and the value of j is being stored in a, then why is a not the same?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
piku_baba
  • 59
  • 7
  • Because `j` is incremented after it is assigned to `a`. (I'd not write the `j` in the first section of the `for` loop control either. It does nothing. I'd either write `for (j = 0; j <= 3; j++)` (and omit the initialization in the variable definition) or `for ( ; j <= 3; j++)`. I'd probably use `< 4` rather than `<= 3` too; it's more orthodox C in general, not least because it is the correct way to iterate of an array of 4 elements. However, these parenthetical observations are tangential to your main concern.) – Jonathan Leffler Oct 20 '18 at 06:53
  • Thanks @JonathanLeffler ! Now my confusion is cleared. :) – piku_baba Oct 20 '18 at 06:59

6 Answers6

1

Till j<=3 condition the value of j is assigned to a but after that when j is incremented by 1 (j==4), it breaks out from the loop as the value of j is now 4 and it didn't assign the value of j to a. So finally we get a=4 and j=3.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
suvojit_007
  • 1,690
  • 2
  • 17
  • 23
1

For j=0 a=0

For j=1 a=1

For j=2 a=2

For j=3 a=3

For j=4loop get terminated as j<=3become false, so j value is 4 and a value is 3.

sahushivam
  • 109
  • 1
  • 6
0

It's due to the way the 3 expressions in the for-loop are executed.

Consider

for( exp1; exp2; exp3)
{
    body;
}

this will be executed like:

exp1;
if (exp2 == false) goto end_of_loop;
body;
exp3;
if (exp2 == false) goto end_of_loop;
body;
exp3;
if (exp2 == false) goto end_of_loop;
body;
exp3;
if (exp2 == false) goto end_of_loop;
. . . 
. . .

end_of_loop:

As you can see exp3 is always executed one time after body.

So in your case j will be incremented one time after it was assigned to a. Like:

 a=j; // part of body
 ++j; // exp3
 j<=3 (exp2) becomes false and the loop ends
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

The for loop checks the condition at every iteration. So when you initialized j=0, gave the condition that the following code be looped till j<=3 while incrementing the value of j after every iteration, the loop does the same.

So for the first iteration, j is 0 and hence the condition is satisfied and a is assigned the value of j. Now the value of j is incremented by 1. It goes on till j=3.

When j=3, a is also equal to 3. Now, the value of j is incremented by 1 and it equals 4. Now the condition is checked. Since j!=3, the loop breaks and you move out of the loop. Hence, although j=4, a is still 3.

Hope it helps.

Prashant Sengar
  • 506
  • 1
  • 7
  • 24
0

1st:

 j = 0 
 j <= 3 => print hello and a = 0, ++j

2nd:

 j = 1
 j <= 3 => print hello and a = 1, ++j

3rd:

 j = 2
 j <= 3 => print hello and a = 2, ++j

4th:

 j = 3
 j <= 3 => print hello and a = 3, ++j

5th:

 j = 4 not satisfy j <= 3

So j = 4 and a =3

T1412
  • 705
  • 1
  • 5
  • 12
0

We should be bit carefull,when we are using prefix incrementation in a looping concept.

For j=0 a=0 # 1st iteration

For j=1 a=1 #2nd iteration

For j=2 a=2 # 3rd iteration

For j=3 a=3 #4th iteration

For j=4 as the condition is not satisfied, control comes out of the for loop. But the j value is incremented earlier so it outputs "4".

Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
Varun
  • 16
  • 2