Basic do while Loop Program - Coding is Funny

Basic do while Loop Program

Basic do while Loop Program

Every loop consists of three parts in sequence:
  1. Initialization: Use to initialize the loop variable.
  2. Condition: It is checked after each iteration as an entry point to the loop.
  3. Updation: Incrementing the loop variable to eventually terminate the loop not satisfying the loop condition.
Do while loop is used when the actual code must be executed atleast once. For example: Incase of menu driven functions.
Below is a simple program on do while loop.
#include<stdio.h>

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    /* 
        always declare the variables before using them 
    */
    int i = 10;     // declaration and initialization at the same time

    do // do contains the actual code and the updation
    {
        printf("i = %d\n",i);
        i = i-1;    // updation
    }
    // while loop doesn't contain any code but just the condition
    while(i > 0);
    
    printf("\n\The value of i after exiting the loop is %d\n\n", i);
    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

Output:


  • Share: