Been trying to figure this one out for the last 3 hours and so hard to find any search results for "nesting for loop inside for loop", so I must ask this question and somebody can help clarify, as much of an amateur question this is, will much appreciate any and all help.
So I have this block of code underneath, for a beginners looping exercise I found at http://www.w3resource.com/php-exercises/php-for-loop-exercises.php on question 3 of that page. The end result should be on that page as well.
<?php
for($a = 1; $a <= 5; $a++) {
for($b = 1; $b <= $a; $b++) {
echo "*";
if($b < $a) {
echo " ";
}
}
echo "<br />";
}
?>
It turns out like a triangle shape made up of 15 little *
I am trying to figure out exactly how this works, as even though the site has an answer, I want to understand so I can write things myself instead of copy pasting.
Below is what I have been able to make out after reading php.net
and also trying to look for a solution on Google.
So, the $a
for
loop starts off as true so loop continues onto the $b FOR
loop, it is true so it will echo *
, At this point $b
is not < $a
as loop is not yet over so the ++
have not been added yet and if
statement is false, so we <br />
and re-run loop.
Second time around. Now $a=2
and for
loop is still true so it will go on to $b
for loop and echo *
. This is where I get confused. The $b
has ++
at the end of its statement, so doesn't it mean that after loop 1 $b = 2
and if
is still false ?
In my head I keep thinking it should print out like:
<br />* <br /> * <br />
instead of this:
<br />* <br /> * * <br />
My conclusion.
$b
value will reset back to 1 after each loop and repeats itself until if
statement is false.OR
*
that get printed are stored and will be the automatic starting point at the beginning of each loop. I understand that if doing a simple print for a for($i=0;$i<11;$i++)
loop will carry the value over, however in my situation, a printed *
is not a value, so it shouldn't be carried over to a new loop.OR
Either way, its been confusing me so badly.
Sorry for long explanation.
Thanks.
Let's take things one step at a time
Outer Loop
for($a = 0; $a <= 5; $a++) {
//inner loop here
echo "<br>"; //after the inner loop has executed, start a new line
}
Start at 0, go up to 5 and do whatever is written inside. Now that means two things will happen for each iteration
1) Inner loop will be executed for every iteration of the outer loop
2) An HTML row break will be printed for every iteration of the outer loop
Now what does the Inner Loop do?
for($b = 1; $b <= $a; $b++) {
echo "*";
if($b < $a) {
echo " "; // $b<$a means we still have more stars to print on this line
}
}
Start at 1, go up to the value of $a
, this $a will come from the outer loop and will be 1 for the first run, 2 for second up to 5. And while doing this, print a *
; that means for the first time 1 star will be printed because loop will start at 1 and end at 1. For the second time 2 stars, 3rd time three stars and so on.
Now after printing the star, see if this is the last star that this iteration had to print, if it is not that then print a space so that the next time a star is printed there is a space in between.
That's about it :) Note that your outer loop starts at 1, it should start at 0 to form the required shape correctly.
Now if you are still confused about the $b<$a
thing, just get rid of it and run your code again and you will notice nothing much has changed except that there are no spaces between the stars and it will be a little easier to understand
for($b = 1; $b <= $a; $b++) {
echo "*";
}
And oh, ++
after a variable means increment its value by 1. In a for loop that means on the next run add 1 to the value of the counter. You can also write it as
for($a=0; $a<=5; $a=$a+1) // thats the same as $a++;
If you wanted the value to increment by more than 1 you could say
for($a=0; $a<=5; $a=$a+2) // a will be 0 , 2, 4