Is there ever a need for a “do {…} while ( )” loop?

Yes I agree that do while loops can be rewritten to a while loop, however I disagree that always using a while loop is better. do while always get run at least once and that is a very useful property (most typical example being input checking (from keyboard))

#include <stdio.h>

int main() {
    char c;

    do {
        printf("enter a number");
        scanf("%c", &c);

    } while (c < '0' ||  c > '9'); 
}

This can of course be rewritten to a while loop, but this is usually viewed as a much more elegant solution.

Leave a Comment