---
title: "do...while versus for/while loops"
subject: "Computer Programming"
chapter: 5
key: "semester_1/computer_programming:do-while-vs-for-while"
source: https://www.bctengineer.com/q/semester_1/computer_programming/do-while-vs-for-while
site: BCT Engineering
---

# do...while versus for/while loops

Computer Programming · Chapter 5: Control statements

- Asked in: 2074 Asoj, 2 marks; 2071 Shrawan, 3 marks (asked 2 times)
- Video: [C_40 Difference between for, while and do while loop in C | C Programming Tutorials](https://www.youtube.com/watch?v=Z3zUwN98uRQ) — Jenny's Lectures CS IT
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/do-while-vs-for-while
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-5

## Question

Differentiate between do...while and for statements. Write the difference between do and do...while loop.

## Short answer

do...while is exit‑controlled, so its body runs at least once; for loop is entry‑controlled with initialisation, condition, and update in one header, typically used when the iteration count is known. do alone is just the keyword; the full construct is do { ... } while (condition).

## Answer

#### do...while versus for
| do...while | for |
|---|---|
| Exit-controlled: condition is tested after the body | Entry-controlled: condition is tested before the body |
| Body executes at least once | Body may not execute at all |
| Initialisation and update are written separately | Initialisation, condition and update are in one line |
| Semicolon is needed after `while(condition);` | No semicolon after the header |
| Used when the number of repetitions is not known and at least one run is needed (menus) | Used when the number of repetitions is known |

#### do versus do...while
`do` is only the keyword that starts the loop; it cannot be used alone. The complete loop is `do { ... } while (condition);`, where `do` starts the body and the `while` clause at the end decides whether to repeat. The (entry-controlled) `while` loop tests first; the `do...while` loop tests after the body.

```c
#include <stdio.h>
int main(void)
{
    int i = 10;
    while (i < 5)
        printf("while body runs: %d\n", i);
    do
    {
        printf("do-while body runs once: %d\n", i);
    } while (i < 5);
    for (i = 1; i <= 3; i++)
        printf("for: %d\n", i);
    return 0;
}
```
Output:
```text
do-while body runs once: 10
for: 1
for: 2
for: 3
```

The `while` loop did not run (10 < 5 is false), but `do...while` ran once.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why is do...while called an exit-controlled loop?
   A. Its condition is tested after the body has executed
   B. It can only be left using break
   C. Its condition is tested before the body
   D. It has no condition at all
   Answer: A. In do...while the while(condition) comes at the end, so the body runs first. (Key point: Exit-controlled)

2. i = 10 and the loop is do { printf(...); } while (i < 5); What happens?
   A. The body never runs
   B. The body runs once, then the condition fails
   C. The body runs forever
   D. The compiler reports an error
   Answer: B. The condition is checked only after the first run of the body, so it executes exactly once. (Key point: At least once)

3. What is the role of the keyword do?
   A. It is a complete loop by itself
   B. It tests the condition first
   C. It only starts the body; the loop needs the closing while clause
   D. It is used in place of for
   Answer: C. do cannot be used alone; the complete loop is do { ... } while (condition); (Key point: do keyword)

4. Which statement needs a semicolon after its condition?
   A. for (i=0; i<3; i++);
   B. while (cond) { ... }
   C. switch (x) { ... }
   D. do { ... } while (cond);
   Answer: D. The do...while statement ends with a semicolon after while(condition). (Key point: Semicolon)

## Related in Computer Programming

- [While loop with syntax and flowchart](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Awhile-loop-syntax-flowchart) — Computer Programming, chapter 5
- [Syntax of while loop](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asyntax-while) — Computer Programming, chapter 5
- [DO versus implied DO loop](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-do-vs-implied-do) — Computer Programming, chapter 11, asked 3×
- [Arithmetic versus logical IF](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-arithmetic-vs-logical-if) — Computer Programming, chapter 11, asked 3×

## Related in other subjects

- [Open loop versus closed loop control](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aopen-vs-closed-loop) — Embedded System, chapter 7, asked 6×
- [Potential difference versus emf](https://www.bctengineer.com/question.md?key=semester_1%2Fbasic_electrical_engineering%3Apotential-difference-vs-emf) — Basic Electrical Engineering, chapter 1
- [Subscriber loop and cable hierarchy](https://www.bctengineer.com/question.md?key=semester_8%2Ftelecommunications%3Asubscriber-loop-cable-hierarchy) — Telecommunications, chapter 1
- [P-f and Q-V control loops](https://www.bctengineer.com/question.md?key=semester_7%2Fpower_plant_equipment%3Apf-qv-control-loops) — Power Plant Equipment, chapter 2, asked 3×
