• Home
  • Posts RSS
  • Comments RSS
  • Edit
  • DO WHILE LOOP vs DO UNTIL LOOP

    Monday, April 11, 2011
    DO WHILE LOOP

    In most computer programming languages, a do while loop, sometimes just called a do loop, is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. Note though that unlike most languages, Fortran's do loop is actually analogous to the for loop.
    The do while construct consists of a block of code and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop, which tests the condition before the code within the block is executed.
    It is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that allows termination of the loop.
    Some languages may use a different naming convention for this type of loop. For example, the Pascal language has a "repeat until" loop, which continues to run until the control expression is true (and then terminates) — whereas a "while" loop runs while the control expression is true (and terminates once the expression becomes false).

    The Do While loop repeatedly executes a section of code while a specified condition continues to evaluate to True. This is shown in the following subroutine, where a Do While loop is used to print out all values of the Fibonacci Sequence until the values exceed 1,000 :
    ' Subroutine to list the Fibonacci series for all values below 1,000
    Sub Fibonacci()

    Dim i As Integer      ' counter for the position in the series
    Dim iFib As Integer       ' stores the current value in the series
    Dim iFib_Next As Integer ' stores the next value in the series
    Dim iStep As Integer ' stores the next step size

    ' Initialise the variables i and iFib_Next
    i = 1
    iFib_Next = 0

    ' Do While loop to be executed as long as the value
    ' of the current Fibonacci number exceeds 1000

    Do While iFib_Next < 1000

        If i = 1 Then
            ' Special case for the first entry of the series
            iStep = 1
            iFib = 0
        Else
            ' Store the next step size, before overwriting the
            ' current entry of the series
            iStep = iFib
            iFib = iFib_Next
        End If

        ' Print the current Fibonacci value to column A of the
        ' current Worksheet
        Cells(i, 1).Value = iFib

        ' Calculate the next value in the series and increment
        ' the position marker by 1
        iFib_Next = iFib + iStep
        i = i + 1
    Loop

    End Sub
    It can be seen that, in the above example, the condition iFib_Next < 1000 is executed at the start of the loop. Therefore, if the first value of iFib_Next were greater than 1,000, the loop would not be executed at all.
    Another way that you can implement the Do While loop is to place the condition at the end of the loop instead of at the beginning. This causes the loop to be executed at least once, regardless of whether or not the condition initially evaluates to True. This makes no difference to the above Fibonacci Sequence loop, as the variable iFib_Next has been initialised to 0 and so the 'While' condition is always satisfied the first time it is tested.
    However, if the variable iFib_Next had previously been used for other calculations and was set to a value greater than zero, the initial 'While' condition would be False, and the loop would not be entered. One way to solve this would be to place the condition at the end of the loop, as follows:
    Do
      .
      .
      .
    Loop While iFib_Next < 1000


    Read more :


    DO UNTIL LOOP


    The Do Until loop is very similar to the Do While loop. The loop repeatedly executes a section of code until a specified condition evaluates to True. This is shown in the following subroutine, where a Do Until loop is used to extract the values from all cells in Column A of a Worksheet, until it encounters an empty cell :
    Do Until IsEmpty(Cells(iRow, 1))

        ' Store the current cell value in the dCellValues array
        dCellValues(iRow) = Cells(iRow, 1).Value
        iRow = iRow + 1

    Loop


    In the above example, since the condition IsEmpty(Cells(iRow, 1)) is at the start of the Do Until loop, the loop will only be entered if the first cell encountered is non-blank.
    However, as illustrated in the Do While loop, you may on some occasions want to enter the loop at least once, regardless of the initial condition. In this case, the condition can be placed at the end of the loop, as follows:
    Do
      .
      .
      .
    Loop Until IsEmpty(Cells(iRow, 1))
    Read more :



    THE DIFFERENCES 


    The difference between "do while" and "do until" is that a "do while" loops while the test case is true, whereas "do until" loops UNTIL the test case is true (which is equivalent to looping while the test case is false). 

    The difference between a "do ...while" loop and a "while {} " loop is that the while loop tests its condition before execution of the contents of the loop begins; the "do" loop tests its condition after it's been executed at least once. As noted above, if the test condition is false as the while loop is entered the block of code is never executed. Since the condition is tested at the bottom of a do loop, its block of code is always executed at least once.


    Read more: http://wiki.answers.com/Q/What_is_the_difference_between_do_while_and_do_until_loop_in_c_programing#ixzz1J9DkDzQ6


    do while loop video !

    do until loop video !

    0 comments: