Home > Mobile >  Loop syntax C language
Loop syntax C language

Time:07-21

I'm looking to print in every line The value of (f) from 10 to 0. I only get it to (2) using thin syntax of Loop I can't understand how this loop works, any hand it will be a great help to me I will be thankful.
I'm using C programming language

int f = 10;
for(f--; f  ; f-=2)
printf("%d\n", f);

CodePudding user response:

A loop of the form

for (setup; condition; repeat) body

is roughly equivalent to:

setup;
while (condition) {
    body;
    repeat;
}

So substitute the parts of your loop into this template and you should be able to see how it works.

int f = 10;
f--;
while (f  ) {
    f -= 2;
    printf("%d\n", f);
}

CodePudding user response:

To get the correct results, you want

for ( int f = 10; f >= 0; f -= 2 )
   printf( "%d\n", f );
  • Related