Print Numbers from 1 to 10 using do while loop
This C program demonstrates how to print the numbers from 1 to 10 using a do-while loop. In a do-while loop, the loop body is executed at least once, regardless of the condition, because the condition is checked after the execution of the loop body.
In this program, a variable i is initialized to 1. The loop begins by printing the value of i and then increments it by 1. The loop continues as long as i is less than or equal to 10. Once i exceeds 10, the loop condition becomes false, and the loop terminates.
The key feature of the do-while loop is that it guarantees the loop body will execute at least once, making it different from a for or while loop, where the condition is checked before entering the loop.
#include <stdio.h>
int main() {
int i = 1; // Initialize the variable to 1
// Start the do-while loop
do {
printf("%d\n", i); // Print the current value of i
i++; // Increment the value of i by 1
} while (i <= 10); // Continue the loop as long as i is less than or equal to 10
return 0; // Exit the program
}
Output-
1
2
3
4
5
6
7
8
9
10do while in C