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

Recommended Answers

All 3 Replies

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).

commented: Happy new year ma'am :) +11

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.

commented: Ha! ha.. HaPpY nEw YeAr sir :) +11
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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.