-1
#include <stdio.h>
int main(){
    int i = 0;

    do {
        i++;
        printf("In while loop\n");
    } while (i < 3);
}

output:

In while loop
In while loop
In while loop

Why the printf statement is executed three times? As soon as the loop starts the value of i becomes 1, so the loop should run 2 times only but it is running 3 times, how?

fede1024
  • 3,099
  • 18
  • 23
Pankaj Mahato
  • 1,051
  • 5
  • 14
  • 26
  • 8
    This may help `printf("i == %d - In while loop\n", i);` – wesley.mesquita Jan 29 '14 at 16:04
  • 2
    it's because you're using a `do { ... } while ();` which is a **post** condition on the loop. So when you'll exit the loop i will be equal to three and the code will have been run with i equal to three. – zmo Jan 29 '14 at 16:07
  • 2
    Don't simply ask for fish, learn how to fish. Such questions won't help you unless you try on your own. – P0W Jan 29 '14 at 16:10
  • You're doing the check after you print. The first time it goes in, it increments i and prints the statement. Then, it checks if i is less than 3 since it is not, it go back to do. The second time around, it increases i again, i is now 2, and it prints the statement. Then, it check again, is i less than 3? Yes, so it goes back to do, increases i and prints the statement again. Then, it check if i is less than 3, but it is not less than 3. It exist the loop. – Josiane Ferice Jan 29 '14 at 16:13
  • 1
    I believe the misconception here is that the while loop will **immediately** end (break) it's execution after the condition is no longer met. That is not the case, as many have said, the condition is checked periodically (in this case, after the print statement). – NX1 Jan 29 '14 at 16:20

6 Answers6

6

Pseudo code:

i = 1
=> In while loop
i = 2
=> In while loop
i = 3
=> In while loop
exit from loop

The condition is checked only at the end, after printf.

fede1024
  • 3,099
  • 18
  • 23
3

The do-while loop tests the condition at the end, so the loop in your example will be executed 3 times with i = 1, 2, 3.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • While the do-while loop does check the condition at the end, that is not the reason this loop runs three times. Whether it was a while or do-while, it would execute three times. – NX1 Jan 29 '14 at 16:12
2

Your condition(i < 3) is checked at the end of the loop.

    1st pass :  i = 1  => "In while loop" printed => (i < 3) satisfied.increment i
    2nd pass :  i = 2  => "In while loop" printed => (i < 3) satisfied.increment i
    3rd pass :  i = 3  => "In while loop" printed => (i < 3) not true.exit from loop

Hope this helps!

Rashmi
  • 81
  • 4
1
  • The first time you hit the while after the first printf i is 1. The loop continues.
  • The second time you hit the while after the second printf i is 2. The loop continues.
  • The third time you hit the while after the third printf i is 3. The loop now ends.

You have hit printf three times.

Andrew Stein
  • 12,880
  • 5
  • 35
  • 43
0

The loop is starting with 'i=0' and then entering the loop with "i++" and becoming 'i=1' then the condition is checked over "i" so in the last loop when 'i=3' the loop is executed and then checked for "i<3".So the loop is repeated thrice.

-7

The reason is u are using post increment.

{

        int i = 0;

        do {

            i++; //first time -  0,second time - 1,third time - 2

            printf("In while loop\n");

        } while (i < 3);

    }

Use pre increment or while instead of do-while to see the difference

arunb2w
  • 1,196
  • 9
  • 28
  • That doesn't make a difference. The value of `i` is changed _before_ the loop condition is tested. There _would_ be a difference in `do { ... } while ( ++i < 3 )` and `do { ... } while ( i++ < 3 )`, though. – Joshua Taylor Jan 29 '14 at 16:08
  • That is why I suggest this "Use pre increment or while instead of do-while to see the difference" also – arunb2w Jan 29 '14 at 16:09
  • If post increment is changed to pre increment in the code in the question nothing changes. Your answer is wrong. – Wojtek Surowka Jan 29 '14 at 16:12