Do-while Loop in C programming language

The do-while loop is similar to the while loop in that the condition is checked after the block of code is executed. This ensures that the code within the loop is run at least once. Similar functionality is provided by C using a while condition and a do loop combination. This construct has the same effect as a standard do-while loop.
 Syntax:-

do {
    // Code to be repeated
} while (condition);
  • Regardless of the condition, the code block contained within the "do" block is executed first.
  • The 'while' condition is assessed following the execution of the code block.
  • The loop will repeat and run the code block once more if the condition is satisfied. The loop will come to an end if the condition is false.

Example:-

#include <stdio.h>
#include<conio.h>

int main() {
    int n;
    
    do {
        printf("Enter a positive number: ");
        scanf("%d", &n);
        
        if (n <= 0) {
            printf("Please enter a positive number.\n");
        }
    } while (n <= 0);

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

    return 0;
}