+ 1

For ( int i=1,i<=4,i++) For (int j=1,j<=i,j++)

Print ("*") I know but how comes explain me in details friend i have always confused in nested loop what is rule for condition in nested for loop * ** *** **** Output this * print but i am still confused explain me details

10th Apr 2020, 8:48 AM
Dharmesh Shukla
Dharmesh Shukla - avatar
7 Réponses
+ 5
for(int i=1; i<=4; i++) runs four times. `i` goes from 1 to 4. for(int j=1; j<=i; i++) runs `i` times. So the inner loop looks like this, step by step: for( i is currently 1 ) for(int j = 1; j <= 1; j++) ...then for( i is currently 2 ) for(int j = 1; j <= 2; j++) ...then for( i is currently 3 ) for(int j = 1; j <= 3; j++) ...then for( i is currently 4 ) for(int j = 1; j <= 4; j++) and we are done.
10th Apr 2020, 8:54 AM
Schindlabua
Schindlabua - avatar
+ 2
Thankx Schindlabua buddy
10th Apr 2020, 8:56 AM
Dharmesh Shukla
Dharmesh Shukla - avatar
+ 1
Dharmesh Shukla Not sure I understand?
10th Apr 2020, 9:11 AM
Schindlabua
Schindlabua - avatar
0
In nested for loop when condition is false Schindlabua
10th Apr 2020, 9:02 AM
Dharmesh Shukla
Dharmesh Shukla - avatar
0
But in second condition j =1 , J<=2 , but here 2 greater than 1 so it's is false condition.. How will it's goes on continue looping Schindlabua explain..!!
10th Apr 2020, 9:22 AM
Dharmesh Shukla
Dharmesh Shukla - avatar
0
j <= 2 is true, because j == 1: 1 <= 2. One is in fact smaller than 2 :) The for loop is doing this, step by step: 1. j = 1 2. print "*" 3. j++ 4. if j <= i go to step 2, else continue 5. we stop. So, for example, in the 3rd iteration (i == 3): 1. j = 1 2. print "*" 3. j++ // j is 2 4. j is smaller or equal than 3, goto step 2 2. print "*" 3. j++ // j is 3 4. j is smaller or equal than 3, goto step 2 2. print "*" 3. j++ // j is 4 4. j is bigger than 3 5. we stop.
10th Apr 2020, 9:33 AM
Schindlabua
Schindlabua - avatar
0
Answer
27th Oct 2024, 5:45 PM
Anish Sharma
Anish Sharma - avatar