How to Use do while Loops in C: Syntax, Examples, and Practical Applications

1. Introduction

The Importance of Looping in C Programming

In programming, “looping”—repeating the same process multiple times—is essential. In C, the for statement, while statement, and, as covered in this article, the do while statement are all used for looping. This article focuses on the do while statement, explaining its basic usage and practical examples.

侍エンジニア塾

2. Basics of the do while Statement

Basic Syntax of the do while Statement

The do while statement guarantees that the loop body will execute at least once. The syntax is as follows:

do {
    // Code to execute
} while (condition);

In this syntax, the code inside the do block is executed first, then the condition is evaluated. If the condition is true, the loop repeats. If it’s false, the loop ends.

Difference from the while Statement

With the while statement, the condition is checked first, and only if it’s true does the loop body execute. In contrast, the do while statement executes the loop body before checking the condition, so it always runs at least once.

3. When to Use do while Statements

Retrying User Input

The do while statement is effective when validating user input. For example, when prompting the user to enter a password, you can use a do while loop to ensure the first input is correct. If the input is invalid, the code prompts the user again—this is simple to implement with do while.

Game Loop Processing

The do while statement is also useful in games, where you want to repeat certain actions until the player chooses to stop. For instance, you can continue looping as long as the player chooses “Continue Game.”

4. do while Statement Code Examples

Basic Example of a do while Statement

Here’s a basic example of how to use a do while statement:

#include <stdio.h>

int main() {
    int i = 0;

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

    return 0;
}

In this example, the code inside the do block executes first, and the loop continues as long as i is less than 5. So, numbers from 0 to 4 are printed.

User Input Example

Here’s an example where the program keeps looping until the user inputs a value within a specific range:

#include <stdio.h>

int main() {
    int number;

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

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

    return 0;
}

In this code, if the user enters a number outside the range 1 to 10, the program will prompt for input again, repeating the loop until a valid number is entered.

5. Infinite Loops and Control

Creating an Infinite Loop

You can create an infinite loop with a do while statement by making the condition always true. An infinite loop keeps repeating forever.

do {
    // Code that repeats forever
} while (1);

How to Control Infinite Loops

To control infinite loops, use break and continue statements:

  • break statement: Used to exit the loop.
  • continue statement: Skips the rest of the loop and starts the next iteration.

Here’s an example using break in an infinite loop:

#include <stdio.h>

int main() {
    int count = 0;

    do {
        printf("Loop count: %d\n", count);
        count++;
        if (count > 5) {
            break;
        }
    } while (1);

    return 0;
}

In this code, the break statement is triggered when count exceeds 5, causing the loop to exit.

6. Using Multiple Conditions in a do while Statement

How to Use Multiple Conditions

In a do while statement, you can combine multiple conditions using logical operators like && (AND) and || (OR).

do {
    // Code to execute
} while (condition1 && condition2);

Example with Complex Conditions

The following example checks whether user input is within the range of 1 to 100. If not, it prompts for input again:

#include <stdio.h>

int main() {
    int number;

    do {
        printf("Please enter a number between 1 and 100: ");
        scanf("%d", &number);
    } while (number < 1 || number > 100);

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

    return 0;
}

This code keeps asking for input until the entered value is between 1 and 100.

7. Summary

The do while statement is useful when you need to ensure the loop body runs at least once. By choosing between while and do while, you can write more flexible programs. You can also control loops with break and continue, and use multiple conditions for advanced scenarios. Mastering these techniques will help you handle loops effectively in C.

8. Practice Problems and Sample Answers

Practice Problem

Write a program that asks the user to enter an integer between 1 and 10, and keeps prompting until a valid input is given. When a valid number is entered, display its double.

Sample Answer

#include <stdio.h>

int main() {
    int number;

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

    printf("Double the entered number: %d\n", number * 2);

    return 0;
}

This practice problem demonstrates how to use a do while loop to validate user input, prompting again for out-of-range values.

9. Related Information and Next Steps

Once you understand the do while statement, it’s helpful to learn how to choose between different loop types (for, while, do while). Another important C programming topic is “pointers.” Understanding pointers enables you to write even more advanced programs, so consider exploring this next.

侍エンジニア塾