While vs Do-While Loop in C
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

While vs Do-While Loop in C Programming – Key Differences Explained

📑 On this page:
  • Introduction
  • What is a while Loop?
  • What is a do-while Loop?
  • Key Differences (Comparison Table)
  • Flowchart Comparison
  • while Loop Program Example
  • do-while Loop Program Example
  • Frequently Asked Questions
📚 In this tutorial, you will learn:
  • What is a while loop and how it works
  • What is a do-while loop and how it works
  • The key differences between while and do-while loops
  • When to use each loop type
  • Complete program examples with explanations

Introduction

In this tutorial section, we will learn the difference between the while loop and the do-while loop in the C programming language.

Both while and do-while loops are types of iterative (looping) statements used in C. However, there are some key differences between them. In this tutorial, we will compare them in a tabular form. But first, let's briefly understand what each loop does.

What is a while Loop?

A while loop in C is a control flow statement that repeatedly executes a block of code as long as a given Boolean condition is true. In other words, a while loop is a type of iterative or repeating statement.

💡 Key Point: The while loop is an entry-controlled loop. It checks the condition before executing the loop body.

What is a do-while Loop?

A do-while loop is known as an Exit-Controlled Loop. It is similar to a while loop, except that it guarantees the loop body will execute at least once.

The main difference is that a do-while loop checks the condition after executing the loop body, whereas a while loop checks the condition before execution.

💡 Key Point: The do-while loop is an exit-controlled loop. It checks the condition after executing the loop body, so it always runs at least once.

Key Differences Between while and do-while Loop

Feature while Loop do-while Loop
Condition Check Checked before execution Checked after execution
Minimum Executions 0 (may not run at all) 1 (always runs at least once)
Loop Type Entry-Controlled Exit-Controlled
Syntax while(condition) do { } while(condition);
Semicolon No semicolon after condition Semicolon required after while(condition);
Curly Braces Optional for single statement Required even for single statement
Initialization Must be done before the loop Can be done before or inside the loop

🎯 Quick Summary: while loop = Check condition first, then execute. do-while loop = Execute first, then check condition.

Flowchart Comparison

The diagram below shows the difference between while and do-while loops in C programming.

Difference between while and do-while loop in C with syntax and flow

Let's break it down and understand how these two loops differ — one step at a time.

1. Syntax:

while loop:

while(condition) {
    // code inside the loop
}

do-while loop:

do {
    // code inside the loop
} while(condition);

2. When the condition is checked:

  • while loop: The condition is checked before the loop starts. So if the condition is false right away, the loop won't run at all.
  • do-while loop: Here, the condition is checked after the loop runs. That means the code inside the loop will run at least once, no matter what.

3. How iteration works:

  • while loop: If the condition is false at the beginning, the loop body won't run.
  • do-while loop: The loop body runs once before checking the condition — so it always runs at least once.

4. Flow of control:

In a while loop, control goes straight to the condition check. In a do-while loop, it first goes to the loop body, then checks the condition. That's the key difference in how they work behind the scenes.

C Program to Illustrate while Loop

The program below displays numbers from 1 to 10 using a while loop.

#include <stdio.h>

int main() {
    int i = 1;
    
    while(i <= 10) {
        printf("\n%d", i);
        i++;
    }
    
    return 0;
}

Sample Output:

1
2
3
4
5
6
7
8
9
10

Explanation:

  1. We declare an integer variable i and initialize it to 1 using int i = 1;.
  2. The program checks the condition in the while loop: i <= 10. Since 1 is less than or equal to 10, the condition is true and the loop begins executing.
  3. Inside the loop, printf("\n%d", i); prints the current value of i, which is 1.
  4. Then, i++; increases the value of i by 1, so i becomes 2.
  5. The control goes back to the top of the while loop and checks the condition again: 2 <= 10. Since it's still true, the loop continues.
  6. This process repeats until i becomes 11. At that point, the condition i <= 10 is false, and the loop stops.

C Program to Illustrate do-while Loop

The program below displays numbers from 1 to 10 using a do-while loop.

#include <stdio.h>

int main() {
    int i = 1;
    
    do {
        printf("\n%d", i);
        i += 1;
    } while(i <= 10);
    
    return 0;
}

Sample Output:

1
2
3
4
5
6
7
8
9
10

Explanation:

  1. The variable i is declared as an integer and initialized to 1 using int i = 1;.
  2. Since this is a do-while loop, the program begins by executing the loop body first — without checking the condition.
  3. The printf("\n%d", i); statement prints 1 to the screen.
  4. Then i += 1; increments the value of i by 1, so now i becomes 2.
  5. After that, the program checks the condition in the while part: i <= 10. Since 2 is less than or equal to 10, the condition is true, and the loop runs again.
  6. This process continues — printing each value of i from 1 to 10 — until i becomes 11. At that point, the condition i <= 10 becomes false, and the loop terminates.
  7. Note: The do-while loop always executes its body at least once, even if the condition is false at the start.

💻 Practice Exercise

Challenge: Write a program using a do-while loop that asks the user to enter a number. The loop should keep asking until the user enters a number greater than 100. Compare this with a while loop implementation.

🔍 Click to Show Solution
#include <stdio.h>

int main() {
    int num;
    
    // Using do-while loop (guarantees at least one execution)
    do {
        printf("Enter a number greater than 100: ");
        scanf("%d", &num);
    } while(num <= 100);
    
    printf("You entered: %d\n", num);
    return 0;
}

📝 Summary

  • while loop: Entry-controlled, checks condition before execution, may run 0 times
  • do-while loop: Exit-controlled, checks condition after execution, always runs at least once
  • while syntax: while(condition) { }
  • do-while syntax: do { } while(condition); (note the semicolon)
  • Choose while when you want to check condition before execution
  • Choose do-while when you want the code to execute at least once

Frequently Asked Questions

1. What is the main difference between while and do-while loop?

The main difference is that a while loop checks the condition first (entry-controlled) and may not execute at all, while a do-while loop checks the condition after execution (exit-controlled) and always executes at least once.

2. When should I use a while loop vs do-while loop?

Use a while loop when you want to check the condition before executing the code (like file reading, user validation). Use a do-while loop when you want the code to run at least once (like menu-driven programs, user input validation where you want to show the prompt first).

3. Is semicolon required in do-while loop?

Yes! The semicolon ; after while(condition) is required in a do-while loop. This is one of the most common mistakes beginners make.

4. Can both while and do-while create infinite loops?

Yes, both can create infinite loops if the condition never becomes false. For example, while(1) or do { } while(1); will run forever.

5. Which loop is better for menu-driven programs?

The do-while loop is typically better for menu-driven programs because you want to display the menu at least once before asking the user to exit.

💡 Tip: Remember: while = "Check first, then do" | do-while = "Do first, then check"

📖 Related Tutorials

  • while Loop in C - Complete Guide
  • do-while Loop in C - Complete Guide
  • Nested while Loop in C
  • Introduction to Loops in C

Previous Topic: -->> Nested while loop in C   ||   Next topic: -->> Difference between goto and loop


📚 Explore More Topics

🗄️ SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Restricting & Sorting Data FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL Subqueries FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Views FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

🐍 Python Interview Questions & Answers

Python Interview Questions Python Syntax & Variables FAQ Python Data Types FAQ Python If-Else FAQ Python Loops FAQ Python Functions Interview Q Python String Manipulation FAQ Python Lists & Dictionaries FAQ Python Tuples & Sets FAQ Python Exception Handling FAQ Python OOP Interview Questions

☕ Java Interview Questions & Answers

Java Introduction Interview Q Java Development Environment FAQ Java Data Types FAQ Java Control Flow & Operators FAQ Java Basic Input/Output FAQ Java Arrays FAQ Java Strings FAQ Java Methods FAQ Java Basic OOP Concepts FAQ Java Advanced OOP Concepts FAQ Java OOP Best Practices FAQ Java Exception Handling FAQ Java Synchronization FAQ Java Threads & Concurrency FAQ Java Collection Framework FAQ Java File I/O & Serialization FAQ Java Serialization & Deserialization FAQ Java Features FAQ Java Inner & Anonymous Classes FAQ Java Memory Management FAQ Java Packages FAQ Java Wrapper Classes FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.