while loop output

I wrote the following while loop :

#include <iostream>
using namespace std;

int main()
{
int p, q, r;

cout << "Enter 3 numbers: ";
cin >> p >> q >> r;

while (p++ <= q--)
{
cout << p << " " << q << endl;
cout << ++r << endl;
}
cout << q + p / r << endl;

return 0;
}

OUTPUT:

Enter 3 numbers: 3 15 1
4 14
2
5 13
3
6 12
4
7 11
5
8 10
6
9 9
7
10 8
8
8

Everything checks out except for the final value being 8. Someone please explain, thanks!
I think it's ur incrementing and decrementing. In the while loop, you do the practically compare p with q then increment them, then that activates your couts within the while loop, causing r to increment. So when you get out the while loop , q would actually be 7, p would be 11 and r would be 8. So when the math is done (it's done in PEMDAS format) it comes out as:
7 + (11/8)
which is:
7 + 1
which results:
8

I think this is how your code is being implemented.
Topic archived. No new replies allowed.