...

/

Discussion: Loop Up and Down

Discussion: Loop Up and Down

Execute the code to understand the output and gain insights into for loops.

Run the code

Now, it's time to execute the code and observe the output.

Press + to interact
#include <stdio.h>
int main()
{
int u,d;
for( u=0, d=0; u<11; u++, d-- )
printf("%2d %2d\n", u, d);
return(0);
}

Understanding the output

The code shows the output of both variables u and d as they ascend and descend from zero:

0 0
1 -1
2 -2
3 -3
4 -4
5 -5
6 -6
7 -7
8 -8
9 -9
10 -10
Code output

A single loop both initializes and increments/decrements each value, setting what could be multiple statements into the for loop statement itself.

Nerdy details on

...