Pages


I made this widget at MyFlashFetish.com.

Saturday, April 9, 2011

Repetition Structure

What are the Difference Between these TWO Repetition Structure

Last week, Madam Sharin Hazlin had told us to post new entry about what are the difference between these two repetition structure
DO......... UNTIL  AND DO.......WHILE?

Repetition Structure : When we talk about allows a user to specify that a program should repeat an action while some condition remains true. 

DO..... UNTIL (DOU)

The DOU operations allow the processing of a group of calculations one or more times. The end of a Do-Until operation is indicated by an ENDDO operation. Here processing is done at least one time.

DO...... WHILE (DOW)

The DOW operations allow the processing of a group of calculations zero or more times. The end of a Do-While operation is indicated by an ENDDO operation.
The do-while is very similar to the regular while with just one difference. The while is a leading decision loop, but the do-while is a trailing decision loop. Therefore the condition is at the beginning of the while loop but at the end of the do-while loop. For that reason a while may be executed zero times, such as when the condition is initially false. On the other hand, the do-while is always executed at least once, because the condition is not even checked until after the first execution of the loop body.
This flowchart illustrates the general logic of a trailing-decision loop. The body of the loop is executed at least once, because the condition test follows the body. A trailing-decision loop can also be implemented using a while loop, which is illustrated in the next exhibit. 

Do...While Example

Suppose you want to verify that the user input was a positive number, and if they enter a negative or zero to keep prompting for correct input. Clearly, in this case you need to get the input before you test for the condition, therefore we need a do-while loop:

int age;
do{
String input = JOptionPane.showInputDialog("Please enter a value for age");
age = Integer.parseInt(input);
}while(age<=0); 


The general for of the do-while statement is:
do
statement 
while(condition);

Source Retrieved On,

No comments:

Post a Comment