Skip to content
StudyFame

Loops in C

Imagine printing "Hello" 100 times. Writing 100 printf lines would be silly. A loop repeats code for you: write it once, run it as many times as you need.

C has three loops: for, while and do-while. This lesson gives you the big picture: what every loop needs, the difference between entry-controlled and exit-controlled loops, and the same program written with all three.

What You Will Learn

  • What a loop is and why we use it.
  • The 4 parts of every loop.
  • The 3 types of loops in C.
  • Entry-controlled vs exit-controlled loops.
  • Which loop to choose when.

Loops in C Explained

Exam definition: A loop is a control statement that executes a block of code repeatedly as long as a condition is true. C has three loops: for, while and do-while. Each repetition is called an iteration.

The 4 parts of every loop

PartJobExample
1. InitializationGive the loop variable its starting valuei = 1
2. ConditionDecide if the loop should run againi <= 5
3. BodyThe work to repeatprintf("%d ", i);
4. UpdateChange the loop variable, moving towards the endi++

Memory trick: "I C B U" = "I Can Be Unstoppable": Initialize, Condition, Body, Update. Forget the Update and your loop really becomes unstoppable (infinite)!

Types of loops

LoopCondition checkedTypeRuns at least once?Best when
forBefore each iterationEntry-controlledNoYou know how many times (count-based)
whileBefore each iterationEntry-controlledNoYou don't know how many times (condition-based)
do-whileAfter each iterationExit-controlledYesThe body must run at least once (menus, input checks)

Entry-controlled vs exit-controlled

  • Entry-controlled (for, while): the condition is checked at the entry gate. If it is false at the start, the body never runs.
  • Exit-controlled (do-while): the condition is checked at the exit gate, after the body. So the body always runs at least once.

Syntax of the Three Loops

for (init; condition; update)     init;                    init;
{                                  while (condition)        do
    body;                          {                        {
}                                      body;                    body;
                                       update;                  update;
                                   }                        } while (condition);

Note the semicolon after while (condition) in a do-while. It is required there, and only there.

Example 1: Print 1 to 5 with All Three Loops

#include <stdio.h>

int main(void)
{
    int i;

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

    printf("\nwhile   : ");
    i = 1;
    while (i <= 5)
    {
        printf("%d ", i);
        i++;
    }

    printf("\ndo-while: ");
    i = 1;
    do
    {
        printf("%d ", i);
        i++;
    } while (i <= 5);

    printf("\n");
    return 0;
}

Output

for     : 1 2 3 4 5
while   : 1 2 3 4 5
do-while: 1 2 3 4 5 

Code Explanation

  1. for: all three control parts (start, condition, update) are on one line: i = 1; i <= 5; i++.
  2. while: i = 1 is before the loop, the condition is in brackets, and i++ is inside the body.
  3. do-while: the body runs first, then while (i <= 5); decides whether to repeat.
  4. All three print 1 to 5, because the condition is true at the start. The difference appears when the condition is false from the beginning (see Example 2).

How a Loop Works (Iteration by Iteration)

  1. Initialize the loop variable (i = 1).
  2. Check the condition (1 ≤ 5? yes).
  3. Run the body (print 1).
  4. Update (i becomes 2).
  5. Go back to step 2. Repeat until the condition becomes false (i = 6), then leave the loop.

Example 2: When the Condition Is False from the Start

#include <stdio.h>

int main(void)
{
    int n = 10;

    while (n < 5)
        printf("while body ran\n");      /* never runs */

    do
        printf("do-while body ran once\n");
    while (n < 5);

    int sum = 0;
    for (int k = 1; k <= 100; k++)
        sum += k;
    printf("Sum of 1 to 100 = %d\n", sum);

    return 0;
}

Output

do-while body ran once
Sum of 1 to 100 = 5050

10 < 5 is false. The while body is skipped completely, but the do-while body runs once before checking. The last loop adds 1 + 2 + ... + 100 = 5050.

Important Points to Remember

  • A loop repeats code while a condition is true.
  • 4 parts: Initialization, Condition, Body, Update.
  • 3 loops: for, while, do-while.
  • for and while are entry-controlled; do-while is exit-controlled.
  • do-while always runs at least once.

Common Mistakes Beginners Make

MistakeWhat happensFix
Forgetting the update (i++)Infinite loopAlways move the loop variable towards the end
for (i = 1; i <= 5; i++);Empty loop; the body runs once after itNo semicolon after for(...)
Missing ; after do-while's while(...)Syntax error} while (cond);
Using < instead of <=Runs one time less (off-by-one)Check the first and last values

Loops in C Practice Questions

  1. What is a loop? Why do we use loops?
  2. Name the three loops in C.
  3. Write the four parts of a loop.
  4. What is the difference between entry-controlled and exit-controlled loops?
  5. Which loop runs at least once even if the condition is false?
Show answers
  1. A statement that repeats a block of code while a condition is true. It avoids writing the same code many times.
  2. for, while, do-while.
  3. Initialization, condition, body, update.
  4. Entry-controlled checks the condition before the body (may run 0 times); exit-controlled checks after the body (runs at least once).
  5. do-while.

Coding Exercise

Print all even numbers from 2 to 20 on one line using any loop.

Show solution
#include <stdio.h>

int main(void)
{
    for (int i = 2; i <= 20; i += 2)
        printf("%d ", i);
    printf("\n");
    return 0;
}
2 4 6 8 10 12 14 16 18 20 

Quick Quiz: Loops in C (MCQ)

  1. Which loop is exit-controlled?

    A) for   B) while   C) do-while   D) None

  2. Which part of a loop prevents it from running forever?

    A) Initialization   B) Update   C) printf   D) Comment

  3. How many times does for (i = 0; i < 5; i++) run?

    A) 4   B) 5   C) 6   D) Infinite

  4. If the condition is false at the start, a while loop runs:

    A) 0 times   B) 1 time   C) Forever   D) Error

  5. Each single run of a loop body is called:

    A) A recursion   B) An iteration   C) A jump   D) A function

Show answers
  1. C: do-while.
  2. B: the update.
  3. B: 5 times (0, 1, 2, 3, 4).
  4. A: 0 times.
  5. B: an iteration.

Quick Revision for Exams and Interviews

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

Must-remember facts

LoopRepeats code while a condition is true
Typesfor, while, do-while
4 partsInitialization, Condition, Body, Update
Entry-controlledfor, while (may run 0 times)
Exit-controlleddo-while (runs at least once)
One run of the bodyIteration
Sum 1 to 1005050

Frequently asked questions

What is the difference between entry-controlled and exit-controlled loops?
Entry-controlled checks the condition before the body; exit-controlled checks it after, so it runs at least once.
When should you use a for loop?
When you know the number of iterations in advance.
When should you use a while loop?
When you don't know how many times, only the condition to stop.
What happens if the update is missing?
The condition never becomes false, so you get an infinite loop.

Remember

  • Loop parts: I-C-B-U.
  • do-while = at least once.

Next Topic

Next lesson: for Loop in C. You will master the for loop with a step-by-step dry run.