简体   繁体   中英

C program incrementing variable with for loop

I am trying to learn the C programming language on my own and have to depend on the internet for some help. I am playing around with one variable and a for loop; incrementing the variable by 1, each iteration of the loop. In this example, I am confused by the fact that the variable is not 1 in the first iteration of the loop. It's like the arguement was skipped on the first pass. I don't understand.

// This is a test of for loops

#include <stdio.h>

main () {

    int a;

    for (a = 0; a < 10; a++) {

        printf("%d\n", a);

    }

    return 0;
}

Maybe it's easiest to understand as follows. In C, a loop written like this:

for (a = 0; a < 10; a++) {
   printf("%d\n", a);
}

is equivalent to this:

a=0;
while (a<10) {
   printf("%d\n", a);
   a++;
}

The for-loop notation is meant to collect up all of the loop control information at the top of the loop as written, but the parenthesized part after the keyword "for" is not executed as a group of statements before the body, it's treated as if it were written as shown in the while loop.

You can also write an infinite loop like this in C:

for (;;) {
   printf("Hello forever\n");
}

which is equivalent to:

while (1) {
   printf("Hello forever\n");
}

You're initializing a to 0 when your loop is created( see a = 0 on line 9 of your code snippet). The first iteration will be just that, 0 .

#include <stdio.h>

int main(int argc, char* argv[]) {

    int a;

    /**
     * RIGHT HERE! 
     * "a" is being assigned the value of 0 for the first iteration, and is incremented each loop
     * until a is no longer less than 10.
     */
    for (a = 0; a < 10; a++) {

        printf("%d\n", a);

    }

    return 0;
}

You could start a at 1 if you really wanted to, but ask yourself "why?" first, as starting from 0 is what you'll find yourself doing most of the time.

(Also, please consider making your main signature compliant with the C standard by specifying the return type and accepting the appropriate arguments eg int main(void) or int main(int argc, char* argv[]) )

#include <stdio.h>

int main (int argc, char **argv)
{
    int c = 0;
    while (c < 10) 
    {
        printf("count: %d\n", c);
        c++;
    }
    return 0;
}

also try initialize c with 1 int c = 1;

and try while (c <= 10)

and you can also use c += 1; , but c++; is usually faster and just for tickles you can try out c = c + 1; , my personal favourite, but that might be the slowest way of incrementing, and your compiler might complain about that one, you'll see!

but whenever one is working with C, one shall use c++; that's just a little joke! ;)

For loop has 4 parts:

for(initialise;condition;updation)
{
  //Body
}

This is how it works:

Step 1: Execute Initialise (Which is zero for you, so a=0)
Step 2: Evaluate Condition: If false,end the loop. If true go to next step. (It's True in your case. The value of a is still 0)
Step 3: Execute Body (value of a is still zero)
Step 4: Execute updation and go back to Step2 (This is where 'a' becomes 1 for your)

This is why for the first time Body is executed, you get the value of a to be zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM