Anonymous · 2015-10-24 14:43
Is it allowed to change the counter variable within a for loop in ANSI C?
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,a;
for (i=1;i<5;++i){
scanf("%d", &a);
if (a%2==0)
printf("value is correct: 2%d\n",a);
else {
printf("try again\n");
i--;
}
}
return 0;
}
Anonymous · 2015-10-24 14:48
Yeah that's fine.
Anonymous · 2015-10-24 16:39
>>10932
You should really try searching for the solutions to your problems before making a thread.
You should also look in the catalog first for threads that may already cover your topic. This would have been more appropriate in the Beginner's General:
https://lainchan.org/%CE%BB/res/5683.html
Now that the thread's here, I suppose it should just be a general C thread.
Anonymous · 2015-10-24 17:01
>>10935
I tried searching for the solution, but found nothing on the interwebs.
Anonymous · 2015-10-24 20:26
>>10938
Yes, but you already had an example you could have tested yourself.
C has a standard, which is really a requirement for any serious language, and if you haven't read it yet I suggest you do. It will really help you as you go on.
Anonymous · 2015-10-25 00:08
>>10932
The i variable is being used for two numbers, iterations and successes. You're better off using a while loop and a meaningful variable name as a counter. break statement to exit the loop.