Wednesday, August 12, 2009

Do While (Repetition Structure) looping

When it comes to looping, it is all about doing the same process over and over again. To do that, we must have something to initiate the loop and something to stop the loop (condition).

Let's start with the While/Do-While loop. A Do While loop is a precondition loop which means before the looping process starts, there is a stated condition whether it should start the looping process or skip it. Similar to the IF/THEN/ELSE they use the decision making system. So if the condition is fulfilled (TRUE), it will start the loop. Else(FALSE) it will skip the loop.

Here is an example of Do While Loop

x=8
WHILE (x>5)
{ statement
statement
statement
x=x-1 (This is to decrease X every loop so that it can stop the loop)
}

When we initialized the loop, you have to do something to stop the loop or else you'll be experiencing and unlimited loop. Notice the x=x-1 in the algorithm? That is to stop the looping process. Before the looping, x has the value 8. It will start the loop as x is bigger than 5. So within the looping process, you need to have something to decrease the value of x until x is smaller than 5 to stop the loop.


After executing the statements fulfilling the condition, the looping process takes it back to the starting point where it checks the condition deciding whether should it continue the loop process or not (Remember to write an instruction to decrease/increase the variable value of the condition to stop the looping process or else you will be getting an unlimited loop).

No comments:

Post a Comment