A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
Every for loop can be rewritten as a while loop. The initialization moves before the while. The condition becomes the while condition. The increment moves to the end of the body. The transformation is mechanical. The question is not whether you can convert. The question is whether you should. for is better when the iteration count is known or the loop variable has a clear progression. while is better when the termination condition is event-driven or the update is conditional.
#include <stdio.h>int main(void) {printf("for loop:\n");for (int i = 0; i < 5; i++) {printf(" i = %d\n", i);}printf("\nEquivalent while loop:\n");int j = 0;while (j < 5) {printf(" j = %d\n", j);j++;}printf("\nfor with continue:\n");for (int i = 0; i < 10; i++) {if (i % 2 == 0) continue;printf(" odd: %d\n", i);}printf("\nwhile with continue (careful):\n");int k = 0;while (k < 10) {k++;if ((k - 1) % 2 == 0) continue;printf(" odd: %d\n", k - 1);}return 0;}
The conversion is straightforward for simple loops. The complication appears with continue. In a for loop, continue jumps to the increment expression. In a while loop, continue jumps to the condition check, skipping the increment. If the increment is after the continue in the while body, it never executes, and you get an infinite loop. The while version must place the increment before any continue, or restructure the logic.
for and while are interchangeable in expressive power. They differ in ergonomics. for keeps the bookkeeping in the header. while keeps it in the body. Choose based on where the complexity lives. Next: returning an array from a function, and the four patterns that make it possible.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.