Mel Script - The do-while loop




A do while loop is a sentinal controlled repetition structure. It is used when you don't know how many times you need to process something. Unlike the while loop, the do-while loop will always execute at least once. The general form is as follows :




		do {
			// action to repeat
		while( <sentinal condition> );



A basic do-while example

This example creates a simple Yes/No confirm dialog. The loop will terminate when the user clicks "Yes". Clicking "No" will cause the loop to continue and the dialog to be re-created. The do-while is more preferable here (even though it is a bit of a noddy example) because the dialog must have been created at least once before the condition of the loop is checked. Doing the equivalent with a while loop would require at least two places in the code where the dialog was created....



		// used to track the response from the dialog.
		string $response="";

		// loop UNTIL the user presses Yes
			$response = `confirmDialog -title "Confirm" 
			              -message "You Must Press Yes"
			              -button "Yes" 
			              -button "No"
			              -defaultButton "Yes"
			              -cancelButton "No"
			              -dismissString "No"`;
		while( $response != "Yes" );