Understanding Simple For Loop Code in C [closed]

So,

int main()
{
   int x=1;           // line 1
   for (;x<=10;x++);  // line 2
   printf("%d\n",x);  // line 3
   return 0;          // line 4
}

Line 1 initializes x to 1.

Line 2 keeps increasing x by 1 until it reaches 11. The first semicolon indicates “don’t do anything before starting the loop”, x<=10 indicates keep going until x > 10 (so when x = 11) and x++ means increase x by 1 each time. If x >= 11, this line gets basically skipped because x is already greater than 10.

Line 3 prints out x to the command line (in this case, x = 11 if x started out less than 11 or just x if x started at >= 11 due to the previous line)

Line 4 means the program was successful, exit the program.

Leave a Comment