🟢 Beginner  ·  Lesson 13

for Loop

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.

for Loop का Syntax

for loop C में सबसे commonly used loop है। इसे तब use करते हैं जब आपको पहले से पता हो कि loop कितनी बार चलना है।

Syntax
for (initialization; condition; update) {
    // Body: जो code repeat करना है
}
// initialization: शुरू में एक बार चलता है → int i = 1
// condition:      हर iteration से पहले check होती है → i <= 10
// update:         हर iteration के बाद चलता है → i++

for Loop कैसे काम करता है

  1. Initialize: int i = 1 — एक बार loop variable set करो
  2. Condition check: i <= 5 — false है तो loop exit
  3. Body execute: { } के अंदर का code run करो
  4. Update: i++ — variable increment/decrement करो
  5. Step 2 पर वापस जाओ

Examples

Example 1: 1 से 10 Print करना

C Language
for (int i = 1; i <= 10; i++) {
    printf("%d ", i);
}
1 2 3 4 5 6 7 8 9 10

Example 2: N तक का Sum

C Language
int n, sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) sum += i;
printf("Sum = %d\\n", sum);

Example 3: Pahada (Multiplication Table)

C Language
int num = 5;
for (int i = 1; i <= 10; i++) {
    printf("%d x %d = %d\\n", num, i, num*i);
}
5 x 1 = 5 5 x 2 = 10 ... (10 तक)

Nested for Loop

एक loop के अंदर दूसरा loop। हर outer loop iteration के लिए inner loop पूरा complete होता है।

Star Pattern
for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= i; j++) printf("* ");
    printf("\\n");
}
* * * * * * * * * *

सारांश

  • for loop तब use करें जब iterations की संख्या पहले से पता हो
  • Syntax: for (init; condition; update) { body }
  • Initialization एक बार चलती है; condition हर iteration से पहले check होती है
  • Common uses: गिनती, sum, tables, arrays, patterns
🏋️ Practice

Programs लिखें: (1) 1 से 100 तक even numbers का sum (2) N तक prime numbers (3) N terms तक Fibonacci series (4) Right-angled star triangle (5) Inverted star pattern।

← Back to C Tutorial