Visit the Official PHP Website

Search this Site

PHP Programming

By James N Hitz

Repetition(Iteration) Control Structures

The do... while Construct

do... while is just a twisted version of while that is more merciful and allows AT LEAST one execution of the statement block - whatever the case. The sytax for this merciful loop is:

do {
	PHP Statement 1;
	PHP Statement 2;
	...
   } while(test expression);

Let's say we have a routine called getPassword() that prompts the user for a password and allows or denies access after 3 attempts. (This is a dummy setup. Don't think so much about it). We would code it like this:

do{
	getPassword();
	$attempts++;
}while($attempts <= 3);

This piece of code will execute once even before checking how many attempts have been made. So in a practice, everyone is allowed at least one attempt. Fair and democratic wouldn't you say.

<< The while Iteration Construct | The for Iteration Construct else >>

JamHitz Productions