php - Multiple nested loop -
can explain me working of multiple nested loops suitable example in php? actually, know how loop inside loop works don't know how loop inside loop of loop works. example trying understand code given below unable understand it's working.
<?php for($i=5;$i>=1;$i--){ for($k=6;$k>=$i;$k--){ echo " "; } for($j=1;$j<=$i;$j++){ echo "* "; } echo "<br>"; }
try debug flow yourself:
the main loop $i
looping 5 times (5 1). each $i
inner first loop $k
looping various times depending on condition , same next loop. check result , debug yourself.
for($i = 5; $i >= 1; $i--){ echo '$i $k started:<br/>'; for($k = 6; $k >= $i; $k--){ echo $i." - ".$k."<br/>"; } echo '<br/><br/>'; echo '$i $j started:<br/>'; for($j = 1; $j <= $i; $j++){ echo $i." - ".$j."<br/>"; } echo "<br/>"; }
result:
$i $k started: 5 - 6 5 - 5 $i $j started: 5 - 1 5 - 2 5 - 3 5 - 4 5 - 5 $i $k started: 4 - 6 4 - 5 4 - 4 $i $j started: 4 - 1 4 - 2 4 - 3 4 - 4 $i $k started: 3 - 6 3 - 5 3 - 4 3 - 3 $i $j started: 3 - 1 3 - 2 3 - 3 $i $k started: 2 - 6 2 - 5 2 - 4 2 - 3 2 - 2 $i $j started: 2 - 1 2 - 2 $i $k started: 1 - 6 1 - 5 1 - 4 1 - 3 1 - 2 1 - 1 $i $j started: 1 - 1
Comments
Post a Comment