Thuta Learning
BasicProgrammingbeginner

For Loop

Relax. We'll talk through this in plain words — no textbook voice.

for loops are easy to use when you already know how many times you need to repeat something. Since initialization, condition, and update all sit on one line, they're a natural fit for counting loops.

c
#include <stdio.h>

int main() {
  for (int i = 1; i <= 5; i++) {
    printf("Step %d
", i);
  }
  return 0;
}

int i = 1 is the starting value, i <= 5 is the condition that checks whether the loop continues, and i++ increases the value after each pass through the loop.

You should see
Step 1 Step 2 Step 3 Step 4 Step 5

Info

When you're looping over an array, remember that indexing starts at 0. For a counting display like this one, though, it's fine to start at 1.

Easy traps

  • The number of times i loops and the number of outputs you expect don't always match — double-check your boundary condition.
For Loop | Thuta Learning