trace

Can someone help me understand how we got the result with this code?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;

void main()
{
	int a[4] = { 10, 20, 30, 40 };
	int i = 0;
	int j = 4;
	while (i++ < 4)
	{
		j = i;
		cout << a[i] << endl;
		while (j-->0)
			cout << i << " ";
	}


     system("pause");
}
since you increment i in the while( i++ < 4 ) line, you never output element 0.
I get that part. i dont get line 16 and 17.
What is the original problem? What is the objective of the program?
no problem and no objective.
im just trying to find out how we get the numbers that the program outputs.
in line 16: j-- is the postfix decrement operator. which means it won't be decremented until after the comparison is made. try it with the prefix decrement while( --j > 0 ) and see if that's what you were expecting.
Topic archived. No new replies allowed.