For Loop


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
   for(int k=4; k>=1; k--)
   {
	   for(int j=k; j>=1; j--)
		   cout<<j<< ' ';
	   cout<<k<<endl;
   }
   
   system("Pause");
   return 0;
}


Can anyone explain to me how this FOR LOOP works ? :)

OUTPUT :
4 3 2 1 4
3 2 1 3
2 2 2
1 1
Last edited on
Round 1: k == 4; so j starts at 4 and counts down 4, 3, 2, 1 (line 10) where it hits the boundary of the j>=1 condition, moving to line 11 that prints the k value of 4. End of round 1

Round 2: k == 3; so j starts at 3 and counts down ..... etc
@gunnerfunner thanks a lot ! that was clear explanation :D
Topic archived. No new replies allowed.