Categories: C

do-while loop in c

The do-while loop in c in an exit controlled loop unlike the for and the while loop the condition is checked at the end of the loop(i.e, after the execution of all the statements inside the do-while block), therefore the do-while loop runs at least once in the program even if the condition fails.

syntax:
do
{
  //statements to be executed
}while(condtion);
Flowchart of the do-while loop:

The do-while loop in c is used in cases where we need to ensure that the statements within the loop are executed at least once.

Example:
#include <stdio.h>

int main() {
  do
  {
      printf("coderzpy.com");
  }while(4<1);
   
    return 0;
}
output:
coderzpy.com

In the above example, even though the condition is false, we get the output as coderzpy.com once.

Note: break and continue statements are used with the do-while as well as the while loop. We shall see further about them in the next post.

Infinite loop with the do-while

The do..while loop can also be used to create the infinite loop. The following is the syntax to create the infinite do..while loop.

do  
{  
    // body of the loop..  
}while(1);  

As we can see the condition will always remain true in the above code, therefore it is an infinite loop.

Note: also read about while loop in c & while loop examples in c

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago