Why is n given the initial value of 1 in this loop?

Posted by notypist on Programmers See other posts from Programmers or by notypist
Published on 2013-11-13T00:25:03Z Indexed on 2013/11/13 4:12 UTC
Read the original article Hit count: 161

Filed under:

From Kochan's "Programming in C":

Input:

printf ("TABLE OF TRIANGULAR NUMBERS\n\n"); 
printf (" n Sum from 1 to n\n");
printf ("--- ---------------\n");
triangularNumber = 0;
for(n=1; n<=10; ++n){ 
    triangularNumber += n;
?    printf (" %i", n);
}

Output (only partly pasted):

TABLE OF TRIANGULAR NUMBERS

n = 1

Sum from 1 to n = 1

n = 2

Sum from 1 to n = 2

Question:

I understand the purpose of this, but here's what I can't get my head around: If within the loop we assign an initial value of "1" to "n", then we check if n<=10, and if that's true (which it should be with the initial value), we then add "1" to n. Shouldn't (and I know it shouldn't, just don't understand why) the initial value displayed in our table be n=2 ?

Thanks in advance for your patience and effort!

© Programmers or respective owner

Related posts about c