Labs ICT
⭐ Pro Login

For Loop

1 min read | C Tutorial
⭐

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

The for loop is the most popular loop in C. It packs initialization, condition, and increment all in one line — neat and compact.

The for Loop Syntax

Three parts inside the parentheses: where to start, how long to go, and how to change each step.


#include <stdio.h>
int main() {
  for (int i = 1; i <= 5; i++) {
    printf("Number: %d\n", i);
  }
  return 0;
}
    

Breaking Down the Pieces

int i = 1 runs once at the start. Then i <= 5 is checked before each iteration. After each iteration, i++ runs. This sequence keeps repeating until the check fails.


#include <stdio.h>
int main() {
  for (int i = 0; i < 10; i += 2) {
    printf("Even: %d\n", i);
  }
  return 0;
}
    

Nested Loops

You can put a loop inside another loop. The inner loop runs completely for each iteration of the outer loop — perfect for tables, grids, or multiplication charts.


#include <stdio.h>
int main() {
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      printf("%d x %d = %d\n", i, j, i * j);
    }
  }
  return 0;
}