Advertisement

Responsive Advertisement

Do while Loop in c

Learning Sections          show

Do-While Loop in C

Example


#include<stdio.h>  // Include the standard input-output library

int main() {
    // Variable declaration
    int count = 0;

    // Do-while loop to print numbers from 1 to 5
    do {
        // Print the current value of count
        printf("Number: %d\n", count);
        
        // Increment count by 1
        count++;
    } while (count <= 5);

    // End of the main function
    return 0;
}
        

Explanation: This program demonstrates the usage of a do-while loop in C. In a do-while loop, the code block inside the loop is executed at least once before the condition is checked. In this example, the loop prints numbers from 0 to 5. It initializes the count variable to 0 and then enters the loop. Inside the loop, it prints the current value of count using the printf function. After printing, it increments the value of count by 1. The loop continues to execute until the condition count <= 5 becomes false.