Lesson 28 of 60 – for Loop in C
47%

for Loop in C

The for loop is used to repeat a block of code multiple times. It is especially useful when you know, or can express, the loop control in terms of an initialization, a condition, and an update.

Note: A for loop has three main parts: initialization, condition, and update. The loop continues while the condition is true.

1. What is a for Loop?

A for loop repeatedly executes a block of statements while its condition remains true.

for (initialization; condition; update)
{
    // statements
}

The loop is commonly used for counting, processing arrays, generating sequences, and repeating tasks.

2. Syntax of for Loop

for (initialization; condition; update)
{
    statement;
}

The three expressions are separated by semicolons.

  • Initialization: sets the starting value.
  • Condition: determines whether the loop continues.
  • Update: changes the loop variable.

3. Simple for Loop

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

Output:

1
2
3
4
5

The loop starts at 1 and continues until i becomes greater than 5.

4. Initialization in for Loop

Initialization usually sets the starting value of the loop variable.

for (int i = 1; i <= 5; i++)
{
    printf("%d ", i);
}

Here, int i = 1 initializes the loop variable to 1.

5. Condition in for Loop

The condition determines whether the loop body should execute.

for (int i = 1; i <= 5; i++)
{
    printf("%d ", i);
}

The body executes while i <= 5 is true.

6. Update in for Loop

The update expression changes the loop variable after each iteration.

for (int i = 1; i <= 5; i++)
{
    printf("%d ", i);
}

The i++ expression increases i by one after each iteration.

7. Execution Flow of for Loop

The basic execution order is:

  1. Initialization executes once.
  2. The condition is checked.
  3. If the condition is true, the loop body executes.
  4. The update expression executes.
  5. The condition is checked again.
  6. The process repeats until the condition becomes false.

8. Printing Numbers from 1 to 10

for (int i = 1; i <= 10; i++)
{
    printf("%d\n", i);
}

This loop prints the numbers from 1 through 10.

9. Printing Numbers from 10 to 1

for (int i = 10; i >= 1; i--)
{
    printf("%d\n", i);
}

The decrement operator -- decreases the value by one after each iteration.

10. Printing Even Numbers

for (int i = 2; i <= 10; i += 2)
{
    printf("%d ", i);
}

Output:

2 4 6 8 10

The update expression increases the value by 2 each time.

11. Printing Odd Numbers

for (int i = 1; i <= 10; i += 2)
{
    printf("%d ", i);
}

Output:

1 3 5 7 9

12. Calculating Sum of Numbers

int sum = 0;

for (int i = 1; i <= 5; i++)
{
    sum = sum + i;
}

printf("Sum = %d", sum);

Output:

Sum = 15

The variable sum stores the running total.

13. Multiplication Table

int number = 5;

for (int i = 1; i <= 10; i++)
{
    printf("%d x %d = %d\n", number, i, number * i);
}

This program generates the multiplication table of 5.

14. for Loop with User Input

#include <stdio.h>

int main()
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++)
    {
        printf("%d ", i);
    }

    return 0;
}

The number entered by the user determines how many numbers are printed.

15. Factorial Using for Loop

int number = 5;
int factorial = 1;

for (int i = 1; i <= number; i++)
{
    factorial = factorial * i;
}

printf("Factorial = %d", factorial);

Output:

Factorial = 120

16. for Loop with Step Value

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

Output:

0 5 10 15 20

The update expression can increase the loop variable by any suitable amount.

17. Decrementing for Loop

for (int i = 10; i >= 0; i--)
{
    printf("%d ", i);
}

Output:

10 9 8 7 6 5 4 3 2 1 0

18. for Loop Without Curly Braces

If a loop contains only one statement, braces may technically be omitted.

for (int i = 1; i <= 5; i++)
    printf("%d ", i);

However, braces are recommended when a loop contains multiple statements or when future modifications are likely.

19. for Loop with Multiple Statements

for (int i = 1; i <= 3; i++)
{
    printf("Number = %d\n", i);
    printf("Hello\n");
}

Both statements execute during every iteration.

20. for Loop with a Condition

for (int i = 1; i <= 10; i++)
{
    if (i % 2 == 0)
    {
        printf("%d ", i);
    }
}

An if statement can be used inside a loop to process only selected values.

21. Nested for Loops

A for loop can be placed inside another for loop. This is called a nested loop.

for (int i = 1; i <= 3; i++)
{
    for (int j = 1; j <= 3; j++)
    {
        printf("%d %d\n", i, j);
    }
}

The inner loop completes its iterations for each iteration of the outer loop.

22. Infinite for Loop

A for loop can be written without a condition:

for (;;)
{
    printf("Running...\n");
}

This creates an infinite loop because there is no condition that becomes false. Such a loop should normally contain a controlled way to terminate, such as a break statement.

23. Omitting Parts of a for Loop

The initialization, condition, and update expressions can be omitted, but the semicolons remain necessary.

int i = 1;

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

This works, although the normal three-part form is usually easier to read for simple counting loops.

24. Common Mistake: Wrong Condition

A wrong condition can make a loop execute too many or too few times.

for (int i = 1; i < 5; i++)
{
    printf("%d ", i);
}

Output:

1 2 3 4

If you want to include 5, use i <= 5.

25. Common Mistake: Forgetting the Update

If a loop variable never changes and the condition remains true, the loop can become infinite.

int i = 1;

for (;;)
{
    printf("%d ", i);
}

Here, i never changes and there is no stopping condition.

A normal counting loop should update its control variable:

for (int i = 1; i <= 5; i++)
{
    printf("%d ", i);
}

26. Common Mistake: Extra Semicolon

An unnecessary semicolon after the for statement creates an empty loop body.

for (int i = 1; i <= 5; i++);

printf("Hello");

The loop above does not repeat the printf() statement. The semicolon is the loop body.

Correct:

for (int i = 1; i <= 5; i++)
{
    printf("Hello\n");
}

27. Complete Sum Program

#include <stdio.h>

int main()
{
    int n;
    int sum = 0;

    printf("Enter a number: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++)
    {
        sum += i;
    }

    printf("Sum = %d", sum);

    return 0;
}

This program calculates the sum of all integers from 1 to the number entered by the user.

28. Complete Multiplication Table Program

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    for (int i = 1; i <= 10; i++)
    {
        printf("%d x %d = %d\n",
               number, i, number * i);
    }

    return 0;
}

29. Complete Factorial Program

#include <stdio.h>

int main()
{
    int number;
    int factorial = 1;

    printf("Enter a positive integer: ");
    scanf("%d", &number);

    if (number < 0)
    {
        printf("Factorial is not defined for negative integers.");
    }
    else
    {
        for (int i = 1; i <= number; i++)
        {
            factorial *= i;
        }

        printf("Factorial = %d", factorial);
    }

    return 0;
}

For larger values, an int can overflow, so appropriate data types and input limits should be considered.

30. Practice Programs Using for Loop

Practice the following programs:

  1. Print numbers from 1 to 100.
  2. Print numbers from 100 to 1.
  3. Print all even numbers from 1 to 100.
  4. Print all odd numbers from 1 to 100.
  5. Calculate the sum from 1 to N.
  6. Calculate the factorial of a number.
  7. Print a multiplication table.
  8. Count the numbers between two limits.
  9. Print multiples of 5.
  10. Create a pattern using nested for loops.

Practice changing the initialization, condition, and update parts to understand how each part controls the loop.

📌 Key Points

  • The for loop is used to repeat a block of code.
  • Its common form contains initialization, condition, and update.
  • Initialization normally runs once before the loop begins.
  • The condition is checked before each iteration.
  • The update expression runs after each iteration.
  • The loop stops when its condition becomes false.
  • The for loop can count upward or downward.
  • It can be used with if statements and nested loops.
  • An omitted condition can create an infinite loop.
  • Always check the loop condition and update to avoid unintended infinite loops.

🧠 Quick Quiz

Question: Which part of a for loop changes the loop variable after each iteration?