[ cyb / tech / λ / layer ] [ zzz / drg / lit / diy / art ] [ w / rpg / r ] [ q ] [ / ] [ popular / ???? / rules / radio / $$ / news ] [ volafile / uboa / sushi / LainTV / lewd ]

λ - programming

/lam/bda /lam/bda duck
Name
Email
Subject
Comment
File
Password (For file deletion.)

BUY LAINCHAN STICKERS HERE

STREAM » LainTV « STREAM

[Return][Go to bottom]

File: 1445697787544.png (10.88 KB, 504x417, for-loop-parts.png) ImgOps iqdb

 No.10932

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;
}

>>

 No.10933

Yeah that's fine.

>>

 No.10935

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

>>

 No.10938

>>10935

I tried searching for the solution, but found nothing on the interwebs.

>>

 No.10945

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

>>

 No.10950

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



Delete Post [ ]
[ cyb / tech / λ / layer ] [ zzz / drg / lit / diy / art ] [ w / rpg / r ] [ q ] [ / ] [ popular / ???? / rules / radio / $$ / news ] [ volafile / uboa / sushi / LainTV / lewd ]