Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What is the difference between while and do while in C?

C
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 26, 2026
6 min read

A while loop tests its condition before each iteration, so its body may run zero times. A do while loop runs the body first and tests the condition afterwards, so its body always runs at least once. That is the whole difference between a while and do while loop in C, and it decides which one fits a given task.

Both repeat code while a condition is non-zero. This guide compares their syntax, shows when each is the right tool and points out the semicolon rule that trips up beginners.

Table of Contents

  • Syntax of while and do while in C
  • The key difference: entry-controlled versus exit-controlled
  • When to use a while loop
  • When to use a do while loop
  • Infinite loops and common mistakes
  • Frequently Asked Questions
    • Which loop runs at least once?
    • Can any while loop be rewritten as do while?
    • Is do while used often in practice?
    • Do I need braces around a one-line body?
  • Summary
  • Related Articles

Syntax of while and do while in C

c

Note the semicolon after the closing parenthesis of do while. It is required there and wrong after a plain while (condition), where it would create an empty loop body.

c
text

With a starting value that satisfies the condition, both loops give the same output. The difference shows when the condition is false from the start.

The key difference: entry-controlled versus exit-controlled

A while loop is entry-controlled: the condition is checked before the body. A do while loop is exit-controlled: it is checked after the body.

c
text

The while loop never runs, because x > 0 is false immediately. The do while loop prints once before it checks the condition and stops.

Featurewhiledo while
Condition checkedBefore the bodyAfter the body
Minimum executions01
Semicolon after conditionNoYes
Typical useUnknown number of repeats, may be nonePrompts, menus, retry until valid

When to use a while loop

Use while when the loop may legitimately do nothing, or when you read until a condition ends the data:

c

getchar returns an int, not a char, so that EOF can be told apart from every valid character. The loop copies standard input to standard output and ends at end of file, possibly without a single pass if the input is empty.

Another common case is processing a number digit by digit:

c
text

If n were 0, the loop would correctly add nothing.

When to use a do while loop

Use do while when the action must happen once before you can decide whether to repeat. The classic example is input validation or a menu:

c

The prompt must appear at least once, and choice has no meaningful value until the user answers, so testing after the body is natural. The inner while discards the rest of a bad line so the loop does not spin forever on non-numeric input. Checking the return value of scanf is important: it returns the number of items read.

The do while form is also used inside multi-statement macros, written as do { ... } while (0), so the macro behaves like one statement and works safely in an if without braces.

Infinite loops and common mistakes

Forgetting to change the variable in the condition produces an infinite loop:

c

Other frequent errors:

  • A stray semicolon, such as while (i < 5);, leaves an empty body and then (when i is never changed) hangs.
  • Missing the final semicolon in do { } while (cond) is a compile error.
  • Using = instead of == in the condition assigns a value, and the compiler usually warns with -Wall.

An intentional infinite loop is written while (1) or for (;;) and left with break or return.

Frequently Asked Questions

Which loop runs at least once?

Only do while. A while loop skips its body entirely if the condition is false at the start.

Can any while loop be rewritten as do while?

Yes, by adding an if check before the loop, though it is rarely clearer. Choose the form that matches how the logic reads.

Is do while used often in practice?

Less often than for and while, but it is the natural fit for menus, prompts and retry logic where one attempt is always needed.

Do I need braces around a one-line body?

They are optional for a single statement, but always using braces avoids bugs when a second line is added later.

Summary

A while loop checks its condition first and may run zero times; a do while loop checks afterwards and always runs once. Remember the semicolon after do while (condition), make sure something in the body changes the condition, and use do while for prompts and retries where the first pass must happen before a decision.

Related Articles

  • How does the for loop work in C?
  • How do break and continue work in C loops?
  • Learn C: Complete Study Roadmap from Beginner to Advanced
Share