Lesson 30 of 60 – do-while Loop in C
50%

do-while Loop in C

The do-while loop is used to repeat a block of code while a condition is true. The important feature of a do-while loop is that the loop body executes at least once, because the condition is checked after the loop body.

Note: In a do-while loop, the statements are executed first and the condition is checked afterward.

1. What is a do-while Loop?

A do-while loop executes a block of statements and then checks whether the condition is true.

do
{
    // statements
}
while (condition);

If the condition is true, the loop repeats.

2. Syntax of do-while Loop

do
{
    statement;
}
while (condition);

Notice the semicolon ; after the while condition. It is required in a do-while loop.

3. Simple do-while Loop

int i = 1;

do
{
    printf("%d\n", i);
    i++;
}
while (i <= 5);

Output:

1
2
3
4
5

4. Execution Order

The execution order of a do-while loop is:

  1. Initialize required variables.
  2. Execute the loop body.
  3. Update values if required.
  4. Check the condition.
  5. If true, execute the body again.
  6. If false, exit the loop.

5. do-while Executes at Least Once

The main difference between while and do-while is that the do-while loop executes its body before checking the condition.

int i = 10;

do
{
    printf("Hello");
}
while (i < 5);

Even though i < 5 is false, Hello is printed once.

6. do-while vs while

while Loop do-while Loop
Condition is checked first. Condition is checked after the body.
May execute zero times. Executes at least once.
No semicolon after the condition. Semicolon is required after the condition.

7. Printing Numbers from 1 to 5

int i = 1;

do
{
    printf("%d ", i);
    i++;
}
while (i <= 5);

Output:

1 2 3 4 5

8. Printing Numbers from 5 to 1

int i = 5;

do
{
    printf("%d ", i);
    i--;
}
while (i >= 1);

Output:

5 4 3 2 1

9. Printing Even Numbers

int i = 2;

do
{
    printf("%d ", i);
    i += 2;
}
while (i <= 10);

Output:

2 4 6 8 10

10. Printing Odd Numbers

int i = 1;

do
{
    printf("%d ", i);
    i += 2;
}
while (i <= 10);

Output:

1 3 5 7 9

11. Calculating Sum

int i = 1;
int sum = 0;

do
{
    sum = sum + i;
    i++;
}
while (i <= 5);

printf("Sum = %d", sum);

Output:

Sum = 15

12. Multiplication Table

int number = 5;
int i = 1;

do
{
    printf("%d x %d = %d\n",
           number, i, number * i);
    i++;
}
while (i <= 10);

This program prints the multiplication table of 5.

13. Factorial Using do-while

int number = 5;
int factorial = 1;
int i = 1;

do
{
    factorial = factorial * i;
    i++;
}
while (i <= number);

printf("Factorial = %d", factorial);

Output:

Factorial = 120

14. Taking User Input

#include <stdio.h>

int main()
{
    int number;

    do
    {
        printf("Enter a positive number: ");
        scanf("%d", &number);
    }
    while (number <= 0);

    printf("You entered: %d", number);

    return 0;
}

The program keeps asking for input until the user enters a positive number.

15. Menu Using do-while

int choice;

do
{
    printf("\n1. Add");
    printf("\n2. Subtract");
    printf("\n3. Exit");
    printf("\nEnter choice: ");
    scanf("%d", &choice);
}
while (choice != 3);

A do-while loop is useful for menus because the menu should normally be displayed at least once.

16. do-while with if Statement

int i = 1;

do
{
    if (i % 2 == 0)
    {
        printf("%d ", i);
    }

    i++;
}
while (i <= 10);

The if statement checks whether each number is even.

17. do-while with Character Choice

char choice;

do
{
    printf("Hello\n");

    printf("Continue? (y/n): ");
    scanf(" %c", &choice);
}
while (choice == 'y');

The loop repeats when the user enters y.

18. do-while for Repeated Input

int number;

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

    printf("You entered %d\n", number);
}
while (number != 0);

The program continues until the user enters 0.

19. do-while with Multiple Statements

int i = 1;

do
{
    printf("Number = %d\n", i);
    printf("Welcome to C programming\n");

    i++;
}
while (i <= 3);

All statements inside the braces execute during every iteration.

20. do-while Countdown

int count = 5;

do
{
    printf("%d\n", count);
    count--;
}
while (count > 0);

printf("Start!");

The loop prints a countdown from 5 to 1.

21. do-while with Logical Operators

int number;

do
{
    printf("Enter number between 1 and 10: ");
    scanf("%d", &number);
}
while (number < 1 || number > 10);

The loop continues while the entered number is outside the valid range.

22. do-while and the Semicolon

The semicolon after the while condition is required.

do
{
    printf("Hello");
}
while (condition);

Without the semicolon, the program will produce a syntax error.

23. do-while with Initial False Condition

int i = 10;

do
{
    printf("%d", i);
    i++;
}
while (i < 5);

Output:

10

The condition is false initially, but the body still executes once.

24. Common Mistake: Forgetting the Update

The loop variable should normally change so that the condition can eventually become false.

int i = 1;

do
{
    printf("%d ", i);
}
while (i <= 5);

Here, i never changes, so the condition remains true and the loop continues indefinitely.

Correct:

int i = 1;

do
{
    printf("%d ", i);
    i++;
}
while (i <= 5);

25. Common Mistake: Wrong Update Direction

The update should move the variable toward the condition becoming false.

int i = 1;

do
{
    printf("%d ", i);
    i--;
}
while (i <= 5);

Here, i becomes smaller while the condition i <= 5 remains true, so the loop does not terminate normally.

26. do-while vs for Loop

for Loop do-while Loop
Initialization, condition, and update are commonly written together. Initialization and update are usually written separately.
Condition is checked before the body. Condition is checked after the body.
Can execute zero times. Executes at least once.

27. Complete Input Validation Program

#include <stdio.h>

int main()
{
    int age;

    do
    {
        printf("Enter your age between 1 and 100: ");
        scanf("%d", &age);
    }
    while (age < 1 || age > 100);

    printf("Valid age: %d", age);

    return 0;
}

This program repeatedly asks for an age until the user enters a value between 1 and 100.

28. Complete Menu Program

#include <stdio.h>

int main()
{
    int choice;

    do
    {
        printf("\n--- MENU ---\n");
        printf("1. Say Hello\n");
        printf("2. Show Message\n");
        printf("3. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        if (choice == 1)
        {
            printf("Hello!\n");
        }
        else if (choice == 2)
        {
            printf("Welcome to C programming!\n");
        }
        else if (choice != 3)
        {
            printf("Invalid choice.\n");
        }

    }
    while (choice != 3);

    printf("Program ended.");

    return 0;
}

29. Complete Sum Program

#include <stdio.h>

int main()
{
    int n;
    int i = 1;
    int sum = 0;

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

    if (n > 0)
    {
        do
        {
            sum += i;
            i++;
        }
        while (i <= n);

        printf("Sum = %d", sum);
    }
    else
    {
        printf("Please enter a positive number.");
    }

    return 0;
}

30. Practice Programs Using do-while

Practice the following programs:

  1. Print numbers from 1 to 10.
  2. Print numbers from 10 to 1.
  3. Print even numbers from 1 to 20.
  4. Print odd numbers from 1 to 20.
  5. Calculate the sum from 1 to N.
  6. Calculate the factorial of a number.
  7. Print a multiplication table.
  8. Ask for a positive number until the user enters one.
  9. Create a menu-driven program.
  10. Ask the user whether to continue using y/n.

Practice these programs to understand why a do-while loop is useful when the statements should execute at least once.

📌 Key Points

  • The do-while loop executes the body before checking the condition.
  • It always executes at least once.
  • The condition is checked after each iteration.
  • A semicolon is required after the while condition.
  • The loop can be used for counting and repeated input.
  • It is useful for menu-driven programs.
  • The loop variable should normally be updated inside the loop.
  • An incorrect update can cause an infinite loop.
  • A false initial condition does not prevent the first execution.
  • The next lesson covers the break statement.

🧠 Quick Quiz

Question: What is the main feature of a do-while loop?