Search⌘ K
AI Features

Discussion: Loop Up and Down

Learn to analyze C for loops by seeing how variables increment and decrement within a single loop statement. Understand the structure of for loops, including initialization, termination, and iteration expressions. This lesson helps you recognize loop output patterns, avoid common errors like empty loops, and grasp nuances of loop execution to write clearer and more effective loop constructs.

Run the code

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

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