Deduce output


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int num=8;
while(num>=0)
{
	if(num % 5 ==0)
	{
		num++;
		continue;
	}
	cout<<num<< " ";
	num=num-2;
}
cout<<endl;
   system("Pause");
   return 0;
}



OUTPUT :

8 6 4 2 1

Can anyone explain to me how this program works and get the following output ? :)
Last edited on
Step through it with a debugger and see what happens.

8 6 4 and 2 are obvious.
When num is 2, num is decremented by 2 and becomes 0.
The loop at line 8 continues because the condition is >= 0.
At line 10, 0 % 5 has no remainder, so line 12 is executed.
num becomes 1 and the loop returns to the top.
line 15 outputs 1 and num is decremented by 2 becoming -1.
The loop stops because num is no longer >= 0.

@AbstractionAnon Thanks for the reply. Thanks a lot :D
Topic archived. No new replies allowed.