Public shame on me (do..while)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Public shame on me (do..while)

Post by Chris Corbyn »

Hi,

How come I never have come across this before? do..while()

It works in PHP and either:

a) Nobody uses it
b) I've lived a sheltered life
c) It's of no real use

I've only come across it since I just started learning C++.

Code: Select all

<?php

$num = 7;

do
{
    echo $num."\n";
} while ($x != 7);

?>
Obviously I'm still learning :D
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

do..while has been there for decades.
Even QBasic has it as DO..LOOP WHILE or DO...LOOP UNTIL
C/C++ had do...while since the beginning
PHP's programming structure is very much like C++ and this has been there right from the first version I guess since PHP is the new language.

Many people use this including me.
This is used when you want a loop to take place ateleast once irrespective of the condition.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

anjanesh wrote:do..while has been there for decades.
Even QBasic has it as DO..LOOP WHILE or DO...LOOP UNTIL
C/C++ had do...while since the beginning
PHP's programming structure is very much like C++ and this has been there right from the first version I guess since PHP is the new language.

Many people use this including me.
This is used when you want a loop to take place ateleast once irrespective of the condition.
Yeah I know what it does I just never seen it used in PHP LOL..

I'm a n00b (at least Jcart calls me a n00b :P)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

My guess is:

do...while isn't taught (either in the classroom or in books) as much as a plain while loop. So people , when thinking of algorithms, usually don't develop their logic in ways that would utilise a do...while loop.

I've honestly only used it twice in 3 years.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

pickle wrote: I've honestly only used it twice in 3 years.
Strange - because many times I have come across situaations where I had to loop it atleast once with no regard to condition for the first time.

I thought these situations occur frequently.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I have known about it for quite some time, but have never actually used it in any software. Although I've seen it a lot, the time never arises when I truly need it ;)
d11wtq wrote:I'm a n00b (at least Jcart calls me a n00b Razz)
:twisted:
Post Reply