do-while Loop - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Sunday 5 November 2017

do-while Loop

do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least one time. The conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.
Syntax :

    do
    {
    statement(s);
    } while( condition );

Example :

    #include <stdio.h>
    int main ()
    {
    // declared local operand (variable)
    int a = 1;

    // do-while loop
    do
    {
    printf("value of a: %d\n", a);
    a = a + 1;
    } while( a < 5 );

    return 0;
    }

Output :
 value of a: 1
 value of a: 2
 value of a: 3
 value of a: 4
One more Example where condition is false :

    #include <stdio.h>
    int main ()
    {
    // declared local operand (variable)
    int a = 1;

    //here, Condition is false. a is not equals to zero
    do
    {
    printf("value of a: %d\n", a);
    a = a + 1;
    } while( a == 0 );

    return 0;
    }
Output :
 value of a: 1

No comments:

Post a Comment

Post Top Ad