Hi,
I have started to study the C language, and as anyone, problems are on.
I am having problems with the for comand for; I do apologise if I am not using the CODE and the ICODE correctly.
here an example:

#include<stdio.h>
int main()
{
  int i;
  int b;
  
  for(i = 1;i<=20;i++)
  {
    b = i * 30;
    printf("b é uguale a: %d = %d * 30\n\n",b, i);
  }
   
   printf("b é uguale a: %d * 30\n", i);
   return 0;
}

If you will run this program after compilation, in the "printf" out the for cicle for will increase the i variable beyond the limit set in the statement.
Why????
Thank you for your help.

Bye Bye

Fab2

Edited 5 Years Ago by __avd: Added [code] tags. Do wrap your programming code blocks within [code] ... [/code] tags

Why????

It might make more sense if you convert to a while loop:

i = 1;

while (i <= 20)
{
    b = i * 30;
    printf("b é uguale a: %d = %d * 30\n\n",b, i);
    ++i;
}

i is always incremented after the body of the loop runs, and when i becomes 21 to fails the condition. So after the loop, i retains the value that failed the condition (ie. 21).

Comments
Happy new year ma'am :)

I do apologise if I am not using the CODE and the ICODE correctly.

This is a new one. Apologizing for not using something correctly that wasn't even attempted. Use Advance Editor would have told you immediately.

Edited 5 Years Ago by WaltP: n/a

Comments
Ha! ha.. HaPpY nEw YeAr sir :)
Thank for your help. Well I am a C beginner and I am trying commands so far learned for studying their different behavior, since I want to get experience about the C language.

Anyway, again thank you.

Bye, bye
This question has already been answered. Start a new discussion instead.