Do while loop example in c language?

Do While Loop Example in C Language

Introduction

The do-while loop is a type of loop in C programming language that is used to execute a block of code repeatedly while a certain condition is true. It is similar to a while loop, but the condition is checked at the beginning of the loop, and the loop body is executed at least once before the condition is checked again.

Basic Syntax

The basic syntax of a do-while loop is as follows:

do {
// code to be executed
} while (condition);

  • do: This keyword starts the do-while loop.
  • : This is the separator between the code to be executed and the condition.
  • while: This keyword starts the while loop.
  • condition: This is the condition that is checked at the beginning of the loop.
  • };: This is the end of the do-while loop.

Example

Here’s an example of a 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 body
do {
printf("Iteration %d: %dn", i, j);
j++;
} while (j < i);

return 0;
}

How it Works

Here’s how the do-while loop works:

  1. The program starts by asking the user to enter the number of iterations.
  2. The user enters the number of iterations, and the program stores it in the variable i.
  3. The program then enters the do-while loop.
  4. The do-while loop checks the condition j < i at the beginning of the loop.
  5. If the condition is true, the program executes the code inside the do block.
  6. The program then increments the variable j and repeats steps 4-5 until the condition j < i becomes false.
  7. Once the condition becomes false, the program exits the do-while loop.

Advantages

The do-while loop has several advantages:

  • Efficient: The do-while loop is more efficient than a while loop because it executes the code inside the loop body at least once before checking the condition.
  • Less Code: The do-while loop requires less code than a while loop because it does not require a separate if-else statement to check the condition.

Disadvantages

The do-while loop also has some disadvantages:

  • Less readable: The do-while loop can be less readable than a while loop because it does not provide a clear indication of the number of iterations.
  • Less flexible: The do-while loop is less flexible than a while loop because it requires the user to enter the number of iterations before the loop starts.

Conclusion

In conclusion, the do-while loop is a powerful tool in C programming that can be used to execute a block of code repeatedly while a certain condition is true. Its efficiency and flexibility make it a popular choice among programmers. However, its less readable and less flexible nature makes it less suitable for complex programs.

Table of Contents

Table

Feature Description
Efficiency The do-while loop is more efficient than a while loop because it executes the code inside the loop body at least once before checking the condition.
Code Readability The do-while loop can be less readable than a while loop because it does not provide a clear indication of the number of iterations.
Flexibility The do-while loop is less flexible than a while loop because it requires the user to enter the number of iterations before the loop starts.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top