The Wayback Machine - https://web.archive.org/web/20230122023733/https://www.geeksforgeeks.org/do-while-loop-in-c/
Skip to content
Related Articles

Related Articles

do…while Loop in C

Improve Article
Save Article
  • Last Updated : 06 Oct, 2022
Improve Article
Save Article

Do-While Loop provides a functionality/feature to recall a set of conditions for a defined number of times. Moreover, this methodology of calling checked conditions automatically is known as a “do-while loop“. The do-while loop is in itself a form of an exit-controlled loop. It is mainly used to traverse arrays, vectors, and other data structures.

Characteristics of the do-while loop in C:

The do-while loop follows a very structured approach where it begins with initializing a condition then checks the condition and in the end executes conditional statements following with the updation of values.

  1. Initialization: This is commonly the first parameter of a fundamental do-while loop that accepts a conditional variable that iterates the value or helps in checking the condition; In Simple words, this initializes or puts value in the variable for future use.
  2. Conditional Statement: It generally accepts 3 parameters(Initialization, Condition, and Updation) that indicate what condition needs to be followed and checked.
    • Check/Test Condition: The Second parameter of a fundamental do-while loop defines the condition that needs to be followed to run the following code statements. In simple terms, if the check expression is true then the iteration of the loop continues otherwise the loop is terminated and further checks(if possible/there) are left unchecked.
    • Updation: The Third parameter of a fundamental do-while loop defines the incrementation or decrementation of the conditional variable that will iterate the code according to the condition.
  3. Body: It is termed as the collection of statements i.e. variables, functions, etc. These are executed automatically after a successful iteration that executes with every successful iteration cycle until the condition is not satisfied. The code statements within the for loop could be anything from printing a simple name to executing complex algorithms or functional statements.
do while loop in C

 

Real-Life Example:

Real-life examples of the do-while loop could be anything by a combination of calling itself for a specified number or infinite times. 

  1. Test driving for a new car uses a do-while loop approach, as we have to drive the car once to know if it fits us or not.
  2. Cleaning of the house until the dust is being washed/removed.
  3. Whenever we fill out a form for a job we have to at least fill in the details for once to check if we are eligible or not.
  4. Whenever we go to a new restaurant we at least order once to check if the food suits us or not.

Syntax:

do  
{ 
// body of do-while loop  
   statements;
}while(1); 

Example:

C




// C program to demonstrate do-while loop
#include <stdio.h>
  
int main()
{
    // Initialization
    int i = 5;
  
    do {
        // statement
        printf("GeeksforGeeks\n");
  
        // Updation
        i++;
    }
    // Check/Test Expression
    while (i < 4);
  
    return 0;
}

Output

GeeksforGeeks

Infinite do while Loop

In this example, the input parameters are not available or do not exist by virtue of which the loop iterates/runs endlessly.

Example:

C




// C program to demonstrate an infinite
// do while loop
#include <stdio.h>
  
int main()
{
    do {
        printf("GeeksforGeeks");
    } while (1);
    return 0;
}

Output:

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
.............

Points to Remember:

  • It is an Exit-Controlled Loop 
  • It can iterate from an adequate number to an infinite number according to the situation.
  • It requires 3 conditions parameters i.e. check expression, conditional statement, and urinary operators for check expression.
  • Its workflow is firstly executing the body and then checking the condition.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!