DO WHILE..ENDDO Loop

Control Structures – DO WHILE..ENDDO Loop

 

/*

Control Structures - DO WHILE..ENDDO Loop

 Second kind of looping structure is DO WHILE..ENDDO Loop. 

 When the repetion count is unknown, FOR...NEXT isn't appropriate for looping; 
 thus, DO WHILE..ENDDO Loop is used for primarily for such situations. 

 Famous 

 DO WHILE .NOT. EOF() 
    ... 
    SKIP 
    ... 
 ENDDO

 loop is probably most used kind of this loop structure. 

 This example is a simple digital watch that run until any key pressed.

 For more information refer here  
*/

PROCEDURE Main()
  CLS

  cTime := TIME()

  nKey := 0

  DO WHILE nKey = 0
    @ 0,70 SAY cTime
    DO WHILE cTime = TIME()
    ENDDO
    cTime := TIME()
    nKey := INKEY()
  ENDDO 

RETURN // Main()

FOR .. NEXT Loop

Control Structures – FOR .. NEXT Loop

 

Nested For..Next loop

/*

Control Structures – FOR .. NEXT Loop

Control structures are code fragments that allow

– execution repeatedly by any number of time or

– if / while a logical condition satisfying by a true value.

All control structures may be nested and always have a beginning  and ending statement.

The first kind of control structures are looping structures and first of them is  FOR..NEXT Loop.

For more information refer here

*/


PROCEDURE Main()

FOR nLines := 1 TO 10
      FOR nColumns := 1 TO 10
         ?? STR( nLines * nColumns, 4 )
      NEXT nColumns 
      ?
   NEXT nLines

   WAIT

RETURN // Main()

UDFs_02 : Calling by reference

/*

UDFs_02 : Calling by reference

In the previous example “cCurrent” was an “argument” ( or actual parameter ), in caller side. In called (invoked / recevied ) side “cTime” is a parameter ( or “formal” parameter).

Though two variables has same value (at the beginning of called function), nothing changed in these definitions, though if two variables has the same name.

In the previous example, “cCurrent” passed by value by caller and “cTime” is a “local” variable as a (“formal”) parameter. So, after terminated the “AmPm” function, “cTime” too erased from memory and became inaccessible.

There is one another method: passing by reference. In this method, caller routine passes (sent) the argument by its reference, not value. In this case called routin uses this formal parameter by its reference, can change  its value and after termination of called routine, argument left alive for caller.

For passing an argument by reference only requirement is adding a “@” sign at the beginning of variable name: AmPm( @cCurrent ), instead of AmPm( cCurrent ).

In this case we haven’t need a return value by called routine, and so called routine haven’t being a FUNCTION.

For more information refer here:

*/

PROCEDURE Main()
cCurrent := "01:43" 

 AmPm( @cCurrent )

 ? cCurrent

 WAIT

RETURN // Main()
*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.
PROCEDURE AmPm( cTime )
IF VAL( cTime ) < 12
 cTime += " am"
 ELSEIF VAL( cTime ) = 12
 cTime += " pm"
 ELSE 
 cTime += STR( VAL( cTime ) - 12, 2 ) +;
 SUBSTR( cTime, 3 ) + " pm"
 ENDIF

RETURN // AmPm()
*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.

UDFs_01 : Defining and calling

/*

UDFs_01 : Defining and calling

Functions and Procedures ( sometimes referred as User Defined Function : UDF )
are basic building block of Harbour programs.

Functions and Procedures are similar, only diffrence is requirments of return value;
by definition, procedures return NIL and functions have a return value.

Practically no difference between two; functions and procedures both may or may not
return value and undefined return value is NIL.

This is a typical example of a user-defined function definition and calling it.

For more information refer here.

*/

PROCEDURE Main()

   cCurrent:= "01:43" 

   ? AmPm( cCurrent)

   WAIT

RETURN // Main()

*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.

FUNCTION AmPm( cTime )

   IF VAL( cTime ) < 12
      cTime += " am"
   ELSEIF VAL( cTime ) = 12
      cTime += " pm"
   ELSE 
      cTime += STR( VAL( cTime ) - 12, 2 ) +;
               SUBSTR( cTime, 3 ) + " pm"
   ENDIF

RETURN cTime // AmPm()

*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.