do while c – do while Loop in C Programming

Do while c: The do while loop in C programming language checks its condition at the bottom of the loop. The block of code is before the condition, so code will be executed at lease one time. A do while loop is similar to a while loop, except the code block of do while loop will execute at least once.

Syntax of do while Loop

do {
    /* code to be executed */
    statement(s);
}while(condition);

 do while Loop Syntax Description

  • condition
    Condition expression is evaluated after code block of do-while loop. It is a boolean expression which decides whether to execute loop’s code again or terminate do-while loop. if condition expression evaluates to true, the code block inside do-while loop gets executed otherwise it will terminate do-while loop and control goes to the next statement after the do-while loop.
  • statement(s)
    do while loop can contain any number of statements including zero statements. All statements inside do-while loop will gets executed atleast once.
  • There is semicolon in the end of while(condition); in do-while loop.

C-Do-While-Loop-Statement

  1. First do while loop execute the codes inside body. Then, the condition expression is tested. If the value of condition expression is true then code block of do while loop(statements inside {} braces) will be executed again otherwise the loop will be terminated.
  2. Do While loop iteration continues unless condition expression becomes false or while look gets terminated using break statement.

C Program to find sum of integers from 1 to N using do while Loop

do while Loop in C Programming

#include <stdio.h>
#include <conio.h>
 
int main(){
    /* 
     * Using do while loop to find the sum 
     * of all integers from 1 to N. 
     */
    int N, counter, sum=0;
    printf("Enter a positive number\n");
    scanf("%d", &N);
    /* Initializing loop control variable before do while loop */
    counter = 1;
    do {
        sum+= counter;
        /* Incrementing loop control variable */
        counter++;
    }while(counter <= N);
    printf("Sum of Integers from 1 to %d = %d", N, sum);
     
    getch();
    return(0);
}

Above program find sum of all integers from 1 to N, using do-while loop.

Output

Enter a positive number
10
Sum of Integers from 1 to 10 = 55

Do While Loop Interesting Facts

  • The condition expression in do while loop is a boolean expression. It must evaluate to true or false value(In C, all non-zero and non-null values are considered as true and zero and null value is considered as false).
  • Unlike for loop, initialization statements, condition expression and update statements are on different line.
  • Opening and Closing braces are not required for single statement inside do while loop code block.
    For Example:
    do
    statement;
    while(condition);
  • We can also use infinite do while loops which will never terminate. You should add terminating condition using break statement in order to terminate infinite do while loop.
    For Example:
    do {
    if(….){
    break;
    }
    } while(1);