php-general Digest 27 Dec 2012 15:42:48 -0000 Issue 8077
Topics (messages 319954 through 319955):
Re: Nested loopa
319954 by: Jim Giner
319955 by: Tedd Sperling
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin 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.
--- End Message ---
--- Begin Message ---
On Dec 26, 2012, at 10:09 AM, Jim Giner <jim.gi...@albanyhandball.com> wrote:
> 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.
> -snip-
> 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.
I too used while's instead of do's for the same reason.
However, in my class I had a student show be the light (one can always learn
from beginners).
Think of it this way, you travel into the code knowing that at some point
you're going to repeat the block of code IF a condition is going to be met
within the block of code. With that consideration, the 'do/while()' works.
Using just a 'while()' for everything means you must determine the what the
truth of the 'while()' is going to be AND set that value before the loop.
Whereas, using a 'do/while()', you don't need to set the truth until the block
of code has been implemented AND at that point determine the truth of the block
of code.
Cheers,
tedd
_____________________
t...@sperling.com
http://sperling.com
--- End Message ---