for Loop in C Programming

A for loop repeats a block of code while its loop condition remains true. It is especially useful when the initialization, condition and update can be written together.

for Loop Syntax Top ↑

for (initialization; condition; update) {
    // statements repeated by the loop
}

Print 0 to 3 Top ↑

#include <stdio.h>

int main(void) {
    for (int i = 0; i <= 3; i++) {
        printf("%d\n", i);
    }

    return 0;
}

Output

0
1
2
3

The variable i is declared inside the for statement, so its scope is limited to the loop.

break and continue Top ↑

break ends the loop immediately. continue skips the remaining statements in the current iteration and moves to the update step.

#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            break;
        }
        printf("%d\n", i);
    }

    return 0;
}

Output

1
2

Change only break to continue:

#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue;
        }
        printf("%d\n", i);
    }

    return 0;
}

Output

1
2
4
5

Change the Step Value Top ↑

#include <stdio.h>

int main(void) {
    for (int i = 0; i <= 25; i += 5) {
        printf("%d\n", i);
    }

    return 0;
}

Output

0
5
10
15
20
25

Nested for Loops Top ↑

#include <stdio.h>

int main(void) {
    for (int row = 1; row <= 3; row++) {
        for (int column = 1; column <= 4; column++) {
            printf("(%d,%d) ", row, column);
        }
        printf("\n");
    }

    return 0;
}

Use different variable names for nested loops so the code is easier to read and the outer-loop value is not hidden.

Reverse a String with a for Loop Top ↑

#include <stdio.h>
#include <string.h>

int main(void) {
    char text[] = "plus2net";
    size_t length = strlen(text);

    for (size_t i = length; i > 0; i--) {
        putchar(text[i - 1]);
    }
    putchar('\n');

    return 0;
}

Output

ten2sulp

Key correction: the last printable character is at length - 1. Starting at length would first access the terminating null character.

Practice with for Loops Top ↑

  • Display the numbers from 1 to n.
  • Display all even and all odd numbers up to n.
  • Find the sum from 1 to n.
  • Find the factorial of an input number.
  • Display the factors of an input number.
  • Reverse the digits of an integer.
  • List prime numbers from 1 to n.
  • Print lower-case and upper-case ASCII tables.
  • Create multiplication tables from 1 to n.

Practice string patterns using for loops

Preserved Example: Value of the Loop Variable After the Loop Top ↑

The original tutorial demonstrated that a loop-control variable declared outside the for statement is still available after the loop and contains the value that caused the loop condition to become false.

#include <stdio.h>

int main(void) {
    int i;

    for (i = 0; i <= 5; i++) {
        printf("i = %d\n", i);
    }

    printf("You came out of the loop.\n");
    printf("i = %d\n", i);
    return 0;
}

Output

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
You came out of the loop.
i = 6




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer