Problem with while loop

Why doesnt it print 9 8 7 6 5 4 3 2 1 0 -1

1
2
3
4
5
6
7
8
9
10
11
12
  #include <iostream>
int main()
{
	int num = 10;
		while (num >= 0)
		{
			--num;
			std::cout << num << std::endl;
			
	}
	return 0;
}
Last edited on
Sorry I meant

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
int main()
{
	int num = 10;
		while (num >= 0)
		{
			
			std::cout << num << std::endl;
			--num;
	}
	return 0;
}
¿why should it? do a desk-test (pencil and paper, go line by line watching all your variables)
v1 does

v2 doesn't because --num is after cout. The while loop is only entered when num >= 0 so the minimum value of num within the loop is 0 which is displayed, then decremented to -1 so the while loop terminates and no further values are displayed.

The difference between 1) and 2) is obviously the position of --num which affects the value displayed.
Topic archived. No new replies allowed.