Do While Loop Example in C
Introduction
The do-while loop is a type of for loop that is used to execute a block of code repeatedly while a certain condition is true. It is a versatile and powerful tool that is widely used in programming languages, including C. In this article, we will explore the do-while loop in C, its syntax, and its usage.
Syntax
The do-while loop is defined as follows:
do {
// code to be executed
} while (condition);
- The do keyword is used to start the loop.
- The while keyword is used to define the condition.
- The condition is a boolean expression that is evaluated before each iteration of the loop.
Example
Let’s consider an example to understand the do-while loop in C.
#include <stdio.h>
int main() {
int i = 0;
int j = 0;
// Initialize variables
printf("Enter the number of iterations: ");
scanf("%d", &i);
// Loop until the condition is true
do {
printf("Iteration %d: ", i);
scanf("%d", &j);
printf("Value of j: %dn", j);
i++;
} while (i < 5);
return 0;
}
How it Works
Here’s a step-by-step explanation of how the do-while loop works:
- The program starts by asking the user to enter the number of iterations.
- The user enters the number of iterations, which is stored in the variable
i. - The program then enters the do loop, which executes the code inside the loop.
- In each iteration of the loop, the program asks the user to enter the value of
j. - The value of
jis stored in the variablej. - The program then increments the value of
iby 1. - The loop continues until the condition
i < 5is false. - At this point, the program exits the do loop and returns to the while loop.
Key Points
- The do-while loop is used to execute a block of code repeatedly while a certain condition is true.
- The condition is evaluated before each iteration of the loop.
- The do keyword is used to start the loop, and the while keyword is used to define the condition.
- The condition is a boolean expression that is evaluated before each iteration of the loop.
- The do-while loop is useful when you want to execute a block of code repeatedly while a certain condition is true.
Advantages
- The do-while loop is more efficient than the while loop because it eliminates the need to check the condition before each iteration.
- The do-while loop is also more concise than the while loop because it eliminates the need to write a separate if statement.
Disadvantages
- The do-while loop can be less readable than the while loop because it does not provide a clear indication of the loop’s termination.
- The do-while loop can be less efficient than the while loop because it does not provide a clear indication of the loop’s termination.
Conclusion
In conclusion, the do-while loop is a powerful and versatile tool that is widely used in programming languages, including C. Its syntax is simple and easy to understand, and its usage is straightforward. The do-while loop is useful when you want to execute a block of code repeatedly while a certain condition is true. Its advantages include efficiency and conciseness, while its disadvantages include lack of readability and efficiency.
