Do while in c example?

Do-While Loop in C: A Comprehensive Guide

What is a Do-While Loop?

A do-while loop is a type of loop in programming that allows you to execute a block of code repeatedly until a certain condition is met. It is similar to the while loop, but with a key difference: the check for the condition is done after the code block is executed, rather than before.

Example of a Do-While Loop in C

Here is an example of a do-while loop in C:

#include <stdio.h>

int main() {
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
printf("n");
return 0;
}

In this example, the code block is executed repeatedly until the condition i < 5 is no longer true. In other words, the loop will continue to execute until i becomes 5.

Key Features of a Do-While Loop

There are two main features of a do-while loop:

  • Initialization: The loop variable is initialized before the first iteration.
  • Condition: The condition is evaluated after each iteration.

Advantages of Do-While Loop

  • Guaranteed execution: The code block is executed at least once, as the condition is evaluated after execution.
  • Easy to read and maintain: The code is easy to read and maintain, as the condition is evaluated after the code block is executed.

Disadvantages of Do-While Loop

  • Less efficient: The condition is evaluated after each iteration, which can be less efficient than other types of loops.
  • More memory usage: The loop requires more memory, as the condition is evaluated after each iteration.

When to Use Do-While Loop

  • When you need to ensure execution of the code block at least once: Use a do-while loop when you want to ensure that the code block is executed at least once, even if the condition is false.
  • When the condition is complex: Use a do-while loop when the condition is complex and cannot be evaluated until after the code block is executed.

Example: Do-While Loop with Input

Here is an example of a do-while loop with input:

#include <stdio.h>

int main() {
char answer;
do {
printf("Do you want to continue? (y/n): ");
scanf(" %c", &answer);
if (answer == 'n') {
break;
}
} while (answer == 'y' || answer == 'Y');
printf("Goodbye!n");
return 0;
}

In this example, the user is prompted if they want to continue. If they enter ‘n’, the loop is terminated. If they enter ‘y’ or ‘Y’, the loop continues.

Conclusion

In conclusion, the do-while loop is a type of loop that allows you to execute a block of code repeatedly until a certain condition is met. It is useful when you need to ensure that the code block is executed at least once, or when the condition is complex and cannot be evaluated until after the code block is executed.

Commonly Confused with Do-While Loop

  • While Loop: The do-while loop is often confused with the while loop, but the key difference is that the condition is evaluated after the code block is executed in a do-while loop.
  • For Loop: The do-while loop is also often confused with the for loop, but the for loop is used for iterating over a range, whereas the do-while loop is used to repeat a block of code until a condition is met.

See Also

  • While Loop: Learn more about the while loop and how it compares to the do-while loop.
  • For Loop: Learn more about the for loop and how it is used in programming.
  • Go to: Learn more about the go to statement and how it is used in programming.

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