do-while loop in C

0


do-while loop in C 



GAIN AND SHINE


Introduction to Do-While Loop in C

    Do-while loop is a type of loop in the C programming language which is used to execute a block of code repeatedly until the given condition is true. It is a post-test loop, which means that the block of code inside the loop will be executed at least once before the condition is checked.


Syntax of Do-While Loop:

The syntax for the do-while loop is as follows:

do {

   // block of code to be executed

} while (condition)

    In this syntax, the block of code inside the curly braces will be executed first, and then the condition will be checked. If the condition is true, the block of code will be executed again. This process will continue until the condition becomes false.


Example of Do-While Loop:

Let's see an example of the do-while loop in C to understand it better:

#include <stdio.h>

   int main() {

   int i = 1;

   

   do {

      printf("%d ", i);

      i++;

   } while (i <= 10);

   

   return 0;

}


    In this example, we have initialized a variable `i` with the value 1. The block of code inside the do-while loop contains a printf statement to print the value of `i`, and then the value of `i` is incremented by 1. The condition of the do-while loop is `i <= 10`. This means that the loop will continue until the value of `i` is less than or equal to 10.


The output of this program will be:

1 2 3 4 5 6 7 8 9 10

Conclusion:

    In conclusion, the do-while loop is a useful construct in C programming language to execute a block of code repeatedly until a given condition is true. It is a post-test loop, which means that the block of code inside the loop will be executed at least once before the condition is checked. It is important to use the do-while loop correctly to avoid infinite loops or unexpected behavior in your program.



Post a Comment

0Comments

Share Your Feedback Here !!

Post a Comment (0)