Labs ICT
Pro Login

For Loop

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 
int main() {
  for (int i = 1; i <= 5; i++) {
    printf("Number: %d\n", i);
  }
  return 0;
}
    
Try it Yourself →

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 
int main() {
  for (int i = 0; i < 10; i += 2) {
    printf("Even: %d\n", i);
  }
  return 0;
}
    
Try it Yourself →

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 
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;
}
    
Try it Yourself →