C5_DO WHILE

DO WHILE
 Execute a loop while a condition is true (.T.)
------------------------------------------------------------------------------
 Syntax

     [DO] WHILE <lCondition>
        <statements>...
        [EXIT]
        <statements>...
        [LOOP]
        <statements>...
     END[DO]

 Arguments

     <lCondition> is the logical control expression for the DO WHILE
     loop.

     EXIT unconditionally branches control from within a DO WHILE or
     FOR...NEXT structure to the statement immediately following the
     corresponding ENDDO or NEXT statement.

     LOOP branches control to the most recently executed DO WHILE or FOR
     statement.

 Description

     DO WHILE...ENDDO is a control structure that executes a block of
     statements repetitively, as long as <lCondition> evaluates to true
     (.T.).  When the condition evaluates to true (.T.), control passes into
     the structure and proceeds until an EXIT, LOOP, or ENDDO is encountered.
     ENDDO returns control to the DO WHILE statement and the process repeats
     itself.  If an EXIT statement is encountered, control branches to the
     nearest ENDDO or NEXT statement.  If a LOOP statement is encountered,
     control branches to the nearest DO WHILE or FOR statement.  If the
     condition evaluates to false (.F.), the DO WHILE construct terminates
     and control passes to the statement immediately following the ENDDO.

     Use EXIT to terminate a DO WHILE structure based on a condition other
     than the DO WHILE condition.  LOOP, by contrast, prevents execution of
     statements within a DO WHILE based on an intermediate condition, and
     returns to the most recent DO WHILE statement.

     DO WHILE constructs may be nested within any other control structures to
     any depth.  The only requirement is that each control structure be
     properly nested.

 Examples

     .  This example demonstrates a typical control structure for a
        simple grouped report:

        LOCAL cOldSalesman, nTotalAmount
        USE Sales INDEX Salesman NEW
        DO WHILE .NOT. EOF()
           cOldSalesman := Sales->Salesman
           nTotalAmount := 0
           DO WHILE cOldSalesman = Sales->Salesman ;
              .AND. (.NOT. EOF())
              ? Sales->Salesman, Sales->Amount
              nTotalAmount := nTotalAmount + Sales->Amount
              SKIP
           ENDDO
           ? "Total: ", nTotalAmount, "for", cOldSalesman
        ENDDO
        CLOSE Sales

     .  This code fragment demonstrates how LOOP can be used to
        provide an intermediate processing condition:

        DO WHILE <lCondition>
           <initial processing>...
           IF <intermediate condition>
              LOOP
           ENDIF
           <continued processing>...
        ENDDO

     .  This example demonstrates the use of DO WHILE to emulate a
        repeat until looping construct:

        LOCAL lMore := .T.
        DO WHILE lMore
           <statements>...
           lMore := (<lCondition>)
        ENDDO

     .  This example uses a DO WHILE loop to move sequentially through
        a database file:

        DO WHILE .NOT. EOF()
           <statements>...
           SKIP
        ENDDO

See Also: BEGIN SEQUENCE DBEVAL() DO CASE FOR IF IF() RETURN



4 responses to “C5_DO WHILE

  1. Pingback: C5 Flow Control | Viva Clipper !

  2. Pingback: C5 Statements | Viva Clipper !

  3. Pingback: C5_IF ( Statement ) | Viva Clipper !

  4. Pingback: C5_FOR | Viva Clipper !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.