While. . .do while structure

Hi,
I'm Ijaj Chowdhury from IUT CEE department.I wanna know when & where "While ....do while" is to be used.Can anyone clear my concept about this?Thank you!!!

Comments

  • The difference between While and Do..While, is that "While" loops check the statements before executing the code inside them, and "Do..While" loops execute the code inside them before checking the statements.

    for example:

    [code]
    #include

    int main(void) {
    int a,b;

    a = 0; b = 4;

    while (b < a) {
    printf("While 1
    ");
    }

    do {
    printf("While 2
    ");
    } while (b < a);

    return 0;
    }
    [/code]

    the first printf never gets displayed because the comparison between a and b is made before executing the instructions inside the loop, and of course 4 < 0 is false.
    The second printf does get displayed because the comparison is made AFTER the code inside the loop is executed once.

    so, when you use "Do..While" loops the code inside them is executed AT LEAST once. That's not necessarily the case with "While" loops.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion