Re: Nested loopa
| From: | Jim Giner | Date: | Wed, 26 Dec 2012 15:09:08 +0000 |
| Subject: | Re: Nested loopa | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-319954@lists.php.net to get a copy of this message | ||
While I fully understand the purpose of the do...while construct, I just never get used to seeing it used. (in other langs I had to deal with a 'repeat...until construct and dis-liked that also). I pretty much know if I'm going to have to deal with a "run at least once" when I'm coding and therefore code appropriately, altho I don't know in what ways I've handled it at this very moment.
That said - here is how I would ahve done your exercise. I just find it easier to read one's code when the conditions are declared up front rather than after reading thru some lines not knowing when or why they run:
<?
$indent = " ";
$a = 0;
while ($a <= 5)
{
echo "A = $a<br>";
$a++;
$b = 0;
while ($b <= 10)
{
echo "$indent B = $b<br>";
$b++;
}
}
To me - so much easier to comprehend at first glance. As soon as my eye comes to a block of code starting with a conditional like 'while', I readily see what makes it tick. Again I see the potential usefulness of doing it the other way as you did in your example, but in your case there wasn't a need for using 'do...while' since you structured it so that there was never a case where you had to force the loop to happen regardless of conditions.