Skip to content
StudyFame

for Loop in C

The for loop is the most used loop in C. It is perfect when you know how many times to repeat: print 1 to 10, add 100 numbers, go through every element of an array.

In this lesson you will learn its syntax and flowchart, and most importantly how to do a dry run: tracing the loop step by step on paper, exactly as exams and interviews expect.

What You Will Learn

  • The syntax and flowchart of the for loop.
  • The order in which its three parts run.
  • How to do a dry run with a table.
  • Sum of N numbers and factorial programs.
  • Useful variations: counting down, steps of 2, empty parts.

for Loop in C Explained

Exam definition: The for loop is an entry-controlled loop that keeps the initialization, condition and update in one line. It is mainly used when the number of iterations is known.

Memory trick: The for loop is like a running track: start line (initialization), "Are you at the finish?" (condition), run a lap (body), step forward (update). The start line is crossed only once.

Syntax of for Loop in C

for (initialization; condition; update)
{
    /* body: repeated while condition is true */
}

Order of execution

  1. Initialization: runs only once, at the very start.
  2. Condition: checked before every iteration. If false, the loop ends.
  3. Body: runs if the condition is true.
  4. Update: runs after the body, then go back to step 2.

Flowchart of the for loop

   [ initialization ]
           |
           v
  +--> < condition? > --No--> [ exit loop ]
  |        | Yes
  |        v
  |     [ body ]
  |        |
  |        v
  +---- [ update ]

Example 1: Sum of First N Natural Numbers Using for Loop

#include <stdio.h>

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

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

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

    printf("Sum of 1 to %d = %d\n", n, sum);
    return 0;
}

Output

Enter N: 5
Sum of 1 to 5 = 15

Dry Run of the for Loop (N = 5)

A dry run means running the program on paper, writing the value of every variable at each step:

IterationiCondition i ≤ 5sum = sum + i
11True0 + 1 = 1
22True1 + 2 = 3
33True3 + 3 = 6
44True6 + 4 = 10
55True10 + 5 = 15
–6False → exitsum stays 15

The loop body ran 5 times, but the condition was checked 6 times: the last check (i = 6) is the one that stops the loop.

How the for Loop Works

  • int i = 1 runs once. (Declaring i inside the for is allowed since C99; then i exists only inside the loop.)
  • Before every iteration, i <= n is checked.
  • After every iteration, i++ moves i one step closer to the end.
  • For a loop for (i = a; i <= b; i++), the body runs b − a + 1 times.

Example 2: Factorial of a Number Using for Loop

#include <stdio.h>

int main(void)
{
    int n;
    long long fact = 1;

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

    for (int i = n; i >= 1; i--)      /* counting down */
        fact = fact * i;

    printf("%d! = %lld\n", n, fact);

    printf("Odd numbers up to 9: ");
    for (int k = 1; k <= 9; k += 2)   /* step of 2 */
        printf("%d ", k);
    printf("\n");

    return 0;
}

Output

Enter a number: 5
5! = 120
Odd numbers up to 9: 1 3 5 7 9 

5! = 5 × 4 × 3 × 2 × 1 = 120. The loop counts down with i--; the second loop jumps in steps of 2 with k += 2. For more practice, see our C program to display a multiplication table.

Important Points to Remember

  • Order: init (once) → condition → body → update → condition ...
  • for is entry-controlled: it may run 0 times.
  • Best when the number of iterations is known.
  • All three parts are optional: for (;;) is an infinite loop.
  • No semicolon right after for (...).

Common Mistakes Beginners Make

MistakeWhat happensFix
for (i = 0; i < 5; i++);Empty loop; body runs once afterRemove the semicolon
Commas instead of semicolons: for (i = 0, i < 5, i++)Syntax errorUse ; between the parts
for (i = 1; i < 10; i--)Infinite loop (i moves away from the end)Update in the right direction
Not initializing sumGarbage resultint sum = 0;
Factorial in an int for big numbersOverflow (13! doesn't fit)Use long long

for Loop Practice Questions

  1. Write the syntax of the for loop and the order in which its parts run.
  2. How many times does the initialization part run?
  3. How many times does for (i = 3; i <= 9; i++) run?
  4. Do a dry run of for (i = 1; i <= 4; i++) p = p * 2; with p = 1.
  5. What does for (;;) do?
Show answers
  1. for (init; condition; update) { body }. Order: init once, then condition → body → update repeatedly.
  2. Only once.
  3. 7 times (9 − 3 + 1).
  4. i=1: p=2; i=2: p=4; i=3: p=8; i=4: p=16; i=5: exit. Final p = 16.
  5. It is an infinite loop (no condition means always true).

Coding Exercise

Read N and print the sum of the squares of 1 to N (1² + 2² + ... + N²). Test with N = 4 (answer 30).

Show solution
#include <stdio.h>

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

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

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

    printf("Sum of squares = %d\n", sum);
    return 0;
}
Enter N: 4
Sum of squares = 30

Quick Quiz: for Loop in C (MCQ)

  1. In a for loop, which part runs only once?

    A) Condition   B) Update   C) Initialization   D) Body

  2. How many times is "Hi" printed? for (i = 0; i < 3; i++) printf("Hi");

    A) 2   B) 3   C) 4   D) 0

  3. Which is an infinite loop?

    A) for (i = 0; i < 5; i++)   B) for (;;)   C) for (i = 5; i > 0; i--)   D) None

  4. Output of for (i = 1; i <= 3; i++); printf("%d", i);

    A) 123   B) 3   C) 4   D) Error

  5. The for loop is:

    A) Exit-controlled   B) Entry-controlled   C) Not a loop   D) Recursive

Show answers
  1. C: initialization.
  2. B: 3.
  3. B: for (;;)
  4. C: 4 (the semicolon makes an empty loop; after it, i is 4).
  5. B: entry-controlled.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Syntaxfor (init; condition; update) { body }
OrderInit once → condition → body → update → repeat
TypeEntry-controlled
Best forKnown number of iterations
Iterations of i = a..bb − a + 1
Infinite forfor (;;)
5!120

Frequently asked questions

What is a dry run?
Running the program on paper, writing the value of every variable at each step.
Are all three parts of a for loop compulsory?
No. All are optional, but the two semicolons are required; for (;;) loops forever.
How many times is the condition checked in a loop that runs n times?
n + 1 times: the last check is false and ends the loop.
Can we declare the loop variable inside for?
Yes, since C99: for (int i = 0; ...); then i exists only inside the loop.

Remember

  • for = running track: start once, check, lap, step.
  • Body runs n times, condition checked n + 1 times.

Next Topic

Next lesson: while Loop in C. You will learn the while loop and use it to reverse a number.