🟢 Beginner  ·  Lesson 13

for Loop

for Loop Syntax

The for loop is the most commonly used loop in C. It's used when you know exactly how many times you want to repeat a block of code.

Syntax
for (initialization; condition; update) {
    // Body: code to repeat
}
// initialization: runs once at start   → int i = 0
// condition:      checked before each iteration → i < 10
// update:         runs after each iteration    → i++

How the for Loop Works

  1. Initialize: int i = 1 — sets up the loop variable once
  2. Check condition: i <= 5 — if false, exit loop
  3. Execute body: run the code inside { }
  4. Update: i++ — increment/decrement the variable
  5. Go back to step 2

Examples

Example 1: Print 1 to 10

C Language
#include <stdio.h>
int main() {
    for (int i = 1; i <= 10; i++) {
        printf("%d ", i);
    }
    printf("\\n");
    return 0;
}
1 2 3 4 5 6 7 8 9 10

Example 2: Sum of First N Numbers

C Language
#include <stdio.h>
int main() {
    int n, sum = 0;
    printf("Enter N: ");
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    printf("Sum of 1 to %d = %d\\n", n, sum);
    return 0;
}
Enter N: 10 Sum of 1 to 10 = 55

Example 3: Multiplication Table

C Language
#include <stdio.h>
int main() {
    int num;
    printf("Enter number: ");
    scanf("%d", &num);
    printf("\\n--- Table of %d ---\\n", num);
    for (int i = 1; i <= 10; i++) {
        printf("%d x %d = %d\\n", num, i, num * i);
    }
    return 0;
}
Enter number: 5 --- Table of 5 --- 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ... (up to 10)

Example 4: Factorial

C Language
#include <stdio.h>
int main() {
    int n;
    long long fact = 1;
    printf("Enter n: ");
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        fact *= i;
    }
    printf("%d! = %lld\\n", n, fact);
    return 0;
}
Enter n: 6 6! = 720

Nested for Loop

A loop inside another loop. The inner loop completes all its iterations for each iteration of the outer loop.

C Language – 2D Pattern
#include <stdio.h>
int main() {
    int rows = 4;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\\n");
    }
    return 0;
}
* * * * * * * * * *

More Pattern Programs

Number Triangle
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= i; j++) {
        printf("%d ", j);
    }
    printf("\\n");
}
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
💡 for Loop Variations

for (;;) — infinite loop (no initialization, condition, update)
for (int i=10; i>=1; i--) — count down
for (int i=0; i<100; i+=5) — step by 5

Summary

  • for loop is used when the number of iterations is known
  • Syntax: for (init; condition; update) { body }
  • Initialization runs once; condition checked before each iteration
  • Nested loops: inner loop runs completely for each outer loop iteration
  • Common uses: counting, summing, tables, arrays, patterns
🏋️ Practice

Write programs: (1) Sum of even numbers 1-100 (2) Print all prime numbers up to N (3) Fibonacci series up to N terms (4) Right-angled star triangle (5) Inverted star pattern.

Frequently Asked Questions

What is a for loop in C?
A for loop repeats a block of code a known number of times. It packs three parts into one line — initialisation, a condition, and an update — such as for (int i = 0; i < 5; i++), which runs the body five times while i goes from 0 to 4.
What are the three parts of a for loop?
The three parts are the initialisation (runs once at the start, e.g. int i = 0), the condition (checked before each pass, e.g. i < n), and the update (runs after each pass, e.g. i++). Together they control how many times the loop runs.
When should I use a for loop instead of a while loop?
Use a for loop when you know how many times to repeat or are counting through a range, because the counter, condition and step sit neatly together. A while loop suits cases where you repeat until some condition changes and the count is not known in advance.
Can you have a for loop without a body in C?
Yes. A for loop can have an empty body if all the work is done in its update part, for example for (i = 0; i < n; sum += a[i], i++);. This is valid but can be harder to read, so it is used sparingly.
What is a nested for loop?
A nested for loop is a loop inside another loop. The inner loop completes all its passes for each single pass of the outer loop, which is why nested loops are used for grids, tables and patterns where you need two levels of counting.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.