September 03, 2018

The do-while Loop in C


   The do-while loop is similar to the while loop except that the loop body will be executed at least once (even if the condition is false), because it first executes the loop body and then it checks the condition.


The syntax of do-while loop is:

do
{
    /* body of the loop */
    statement block;
  
}while(test_condition);


Here first the loop body is executed and then the test condition is evaluated. If the test condition is true, then again the loop body is executed and this process continues as long as the test condition is true. When the test condition becomes false the loop terminates.



Note :
  • The braces are needed only if the body of the loop contains more than one statement. However, it is a good practice to use braces even if the body of the loop contains only one statement.
  • The do-while loop must be terminated with a semicolon after the closing parenthesis of the test condition.


Flow Diagram:


The do-while Loop


Example 1:

The following program uses do-while loop to print numbers from 1 to 10.

#include <stdio.h>

int main(void)
{
    int i=1;
   
    do
    {
        printf("%d\t", i);
       
        ++i;
    }while(i<11);
   
    return 0;
}


Output:

1   2   3   4   5   6   7   8   9   10

Explanation:

In this do-while statement, the two statements printf("%d\t", i); and ++i; are executed, regardless of the initial value of i. Then the condition i<11 is tested, if it is true, the statement body is executed again and i<11 is reevaluated. The statement body is executed repeatedly as long as i remains less than 11. Execution of the do-while statement terminates when i becomes 11. The body of the loop is executed at least once.


Example 2:

The following program uses do-while loop to read a character until a * is encountered and also counts the number of upper case, lower case, and numbers entered by the users.

#include <stdio.h>


int main(void)
{
    char ch;
    int lowerCase = 0, upperCase = 0, numbers = 0;

   
    printf("Enter any character : ");
    scanf("%c", &ch);

    do
    {
        if(ch >= 'A' && ch <= 'Z')
            upperCase++;
        if(ch >= 'a' && ch <= 'z')
            lowerCase++;
        if(ch >= '0' && ch <= '9')
            numbers++;

        printf("Enter another character (or * to exit) : ");
        scanf(" %c", &ch);

    }while(ch != '*');

    printf("\nTotal count of lower case characters entered = %d", lowerCase);
    printf("\nTotal count of upper case characters entered = %d", upperCase);
    printf("\nTotal count of numbers entered = %d", numbers);

   
    return 0;
}


Output:

Enter any character : J
Enter another character (or * to exit) : u
Enter another character (or * to exit) : S
Enter another character (or * to exit) : t
Enter another character (or * to exit) : 9
Enter another character (or * to exit) : C
Enter another character (or * to exit) : 3
Enter another character (or * to exit) : 2
Enter another character (or * to exit) : *

Total count of lower case characters entered = 2
Total count of upper case characters entered = 3
Total count of numbers entered = 3


Explanation:

In this do-while statement, the statement body is executed first, regardless of the initial value of ch. Then the condition ch != '*' is tested, if it is true, the statement body is executed again and ch != '*' is reevaluated. The statement body is executed repeatedly as long as the condition is true. When the condition ch !='*' becomes false the loop terminates.


Where should I use do-while loop ?

In for and while loops, the test condition is evaluated at the beginning of the loop. If the test condition is false when the loop is entered, the loop body won't be executed at all. In some situations this is what you want. But sometimes you want to guarantee that the loop body is executed at least once, no matter what the initial state of the test condition. When this is the case you should use the do-while loop, which places the test condition at the end of the loop. For example, in the below program it is better to use a do-while loop.

/* Program to count digits in a number. */
#include <stdio.h>

int main(void)
{
    int n, count=0, rem;
   
    printf("Enter a number : ");
    scanf("%d", &n);
   
    do
    {
        n = n/10;
        count++;
    }while(n>0);
   
    printf("Number of digits = %d\n", count);
   
    return 0;
}


Output:

Enter a number : 456
Number of digits = 3


Explanation:

In this example, if we write the same program using a while loop then we won't get correct answer when input is 0.

while(n>0)
{
    n=n/10;
    count++;
}


The count of digits in number 0 is 1, but using while loop the answer will come out to be zero.



next    Nesting of Loops in C
top    Index
prev    The for Loop in C



No comments:

Post a Comment