I was printing a pattern like this and found this code online.
AAAAA*
AAAA***
AAA*****
AA*******
A*********
When i calculated how the loop is working I found this that for first iteration the values of i,j and k are 1,5,1 respectively that means five "A" then *;for second loop I calculated the values 2,4,2 respectively.The second loop will run till j>=i which means 4,3,2 as i has been 2 now and j is 4 so there should be 3 "A" not four.Can you help me understand where my thought process is going wrong or how i am calculating their value wrong.
I replaced the cout statements within the nested for loops with the current values of i and j, and i and k. I don't know if this will help visualize what's happening in the loop processing.
i 1 j 5
i 1 j 4
i 1 j 3
i 1 j 2
i 1 j 1
i 1 k 1
i 2 j 5
i 2 j 4
i 2 j 3
i 2 j 2
i 2 k 1
i 2 k 2
i 2 k 3
i 3 j 5
i 3 j 4
i 3 j 3
i 3 k 1
i 3 k 2
i 3 k 3
i 3 k 4
i 3 k 5
i 4 j 5
i 4 j 4
i 4 k 1
i 4 k 2
i 4 k 3
i 4 k 4
i 4 k 5
i 4 k 6
i 4 k 7
i 5 j 5
i 5 k 1
i 5 k 2
i 5 k 3
i 5 k 4
i 5 k 5
i 5 k 6
i 5 k 7
i 5 k 8
i 5 k 9
@wildblue sorry for the noob question but can u explain me why for the second loop the value for j start from 5 shouldn't it start from 4 as j--. Why isn't it value updated to 4.Also why does k start from 0 always.it would be really helpful if you can explain me or point me somewhere about how the variable updation is happening.
For each time through the outside loop (i), the inside loops run fully from their initial value, that's 5 for j and 1 for k, through whatever number they end with in that particular run (which will depend on the current value of i).
1 2 3 4 5 6 7 8 9
i = 1 // start outside loop
first nested loop j=5 and decrements, 4, 3, 2, 1, 0; at 0 loop ends, condition j>=i no longer true
second nested loop k=1 and increments, 2; at 2 loop ends, condition k<2*i (2) no longer truenew line
i = 2 // start outside loop again. Internal loops start back at their beginning state.
first nested loop j=5 and decrements, 4, 3, 2, 1; at 1 loop ends, condition j>=i no longer true
second nested loop k=1 and increments, 2, 3, 4; at 4 loop ends, condition k<2*i (4) no longer truenew line
etc.