Environment Variables

/*

GETENV() is an environment function that retrieve information
from the DOS environment into an application program.

Typically, this is configuration information, including path names,
that gives the location of files (database, index, label, or reports).

*/

PROCEDURE Main()
 ? " Computer Name:", GETENV( "COMPUTERNAME" )
 ? " Home Drive :", GETENV( "HOMEDRIVE" ) 
 ? " Home Path :", GETENV( "HOMEPATH" ) 
 ? " LogOn Server :", GETENV( "LOGONSERVER" ) 
 ? " Number of Processors :", GETENV( "NUMBER_OF_PROCESSORS" ) 
 ? " Processor Architecture :", GETENV( "PROCESSOR_ARCHITECTURE" ) 
 ? " Processor Identifier :", GETENV( "PROCESSOR_IDENTIFIER" )
 ? " Processor Level :", GETENV( "PROCESSOR_LEVEL" )
 ? " Processor Revision :", GETENV( "PROCESSOR_REVISION" )
 ? " OS :", GETENV( "OS" )
 ? " Session Name :", GETENV( "SESSIONNAME" ) 
 ? " User Domain :", GETENV( "USERDOMAIN" ) 
 ? " User Name :", GETENV( "USERNAME" )
 ? " User Profile :", GETENV( "USERPROFILE" )
 ? " Command Specification:", GETENV( "COMSPEC")
 ? " TEMP Folder :", GETENV( "TEMP" )
 ? " TMP Folder :", GETENV( "TMP" )
 ? " WINDOWS Folder :", GETENV( "WINDIR" )
 ? " Program Files Folder :", GETENV( "PROGRAMFILES" ) 
 ? " Prog. files extentions :", GETENV( "PATHEXT" )
 ? " Default Path :", GETENV( "PATH" )
 ?
RETURN // Main()

Variables

/*

Variables are place-holders for data units with variable values;

their value may change while program running.

Variables have “type”, depending value of holded data item.

Type of variables haven’t be declared or defined previously; type of a variable determined
“on the fly” ( automatically ) by system when a value assigned (even changed by type) to
this variable.

Variables have also “scope” (or visibility) and this is most important issue about variables.

This example demonstrate defining and usage of variables in basic manner.

*/

PROCEDURE Main()

LOCAL cExclusiveForPMain := "The variable 'cExclusiveForPMain' is 'LOCAL' in the " +;
 "Main() procedure; thus inaccessible outside of Main()"

 SETMODE( 60, 300 )
 CLS

 PUBLIC cPublicizedInMain := "The variable 'cPublicizedInMain' declared as 'PUBLIC' "+ ;
 " in the Main () procedure; thus is accessible everywhere"

 PRIVATE cPrivatizedInMain := "The variable 'cPrivatizedInMain' declared as 'PRIVATE' " +;
 " in the Main() procedure;" + ; 
 " thus is accessible everywhere"

 cUndeclaredInMain := "The variable 'cUndeclaredInMain' is UN-declared but assigned value " +;
 " in the Main() procedure;" + ; 
 " thus is PRIVATE (by default) and accessible everywhere"
 ?
 ? "In the procedure", PROCNAME(), ":" 
 ? cExclusiveForPMain
 ? cPublicizedInMain 
 ? cPrivatizedInMain
 ? cUndeclaredInMain 

 SubProcL1()

 ?
 ? "In the procedure", PROCNAME(), "(after returned SubProcL1() ) :" 
 ? "If attempted accessing to 'cExclusiveForPSubProcL1' here, a run time error " + ;
 " occurs: Variable doesn't exist" 
 ? cPublicizedInMain 
 ? cPrivatizedInMain 
 ? cUndeclaredInMain 
 ? "If attempted accessing to 'cExclusiveForPSubProcL1' here, a run time error " +; 
 "occurs: Variable doesn't exist"
? cPublicizedInTheSub

RETURN // Main()
PROCEDURE SubProcL1() // Sub procedure Level : 1
LOCAL cExclusiveForPSubProcL1 := "The variable 'cExclusiveForPSubProcL1' is 'LOCAL' " + ;
 "in the SubProcL1() procedure;" + ; 
 " thus inaccessible outside of SubProcL1()"

 ?
 ? "In the procedure", PROCNAME(), ":" 
 ? "If attempted accessing to 'cExclusiveForPMain' here, a run time error " + ;
 "occurs: Variable doesn't exist"
? cPublicizedInMain 
 ? cPrivatizedInMain 
 ? cUndeclaredInMain 
 ? cExclusiveForPSubProcL1

 cPublicizedInMain := "New value assigned in the SubProcL1() to PUBLIC " + ;
 "variable 'cPublicizedInMain' "
cPrivatizedInMain := "New value assigned in the SubProcL1() to PRIVATE variable " +; 
 "'cPrivatizedInMain' "
cUndeclaredInMain := "New value assigned in the SubProcL1() to UNDECLARED PRIVATE " + ;
 " variable 'cUndeclaredInMain' "
cExclusiveForPSubProcL1 := "New value assigned in the SubProcL1() to LOCAL variable " + ;
 "'cExclusiveForPSubProcL1' " 

 ? cPublicizedInMain 
 ? cPrivatizedInMain 
 ? cUndeclaredInMain 
 ? cExclusiveForPSubProcL1

 PUBLIC cPublicizedInTheSub := "The variable 'cPublicizedInTheSub' declared as 'PUBLIC' "+ ;
 " in the SubProcL1() procedure;" + ; 
 " thus accessible outside of SubProcL1()" + ;
 " (of course AFTER it is builded)"
 ? cPublicizedInTheSub 

RETURN // SubProcL1()

Constants

/*

Constants are data units with fixed values; their value never change while program running.

Format of constants sligthly different depending their data type.

This example demonstrate defining and usage of basic constants.

*/

PROCEDURE Main()

   CLS

   SET DATE GERM

   // String constants are strings enclosed by quotation marks ( "",'',[] )
   ? "String :", "This is a string" 

   // Numeric constants are strings of digits, decimal point (.)
   // and sign mark (+/-)
   ? "Numeric :", -123.56  

   // Logical constants are T or F enclosed by "." 
   ? "Logical :", .T. 

   // String constant converted to date type by CTOD() function
   ? "Date :", CTOD( "23.04.2012" ) 

RETURN // Main()

DO CASE..ENDCASE

Control Structures – Decision making; DO CASE..ENDCASE

Do Case

/*

Control Structures - Decision making; DO CASE..ENDCASE

For more information refer here 

*/

PROCEDURE Main()
   CLS

   nKey := 0

   cDash := ''

   ? "Press a digit key ( Esc for exit ) : " 

   DO WHILE nKey # 27

      nKey := INKEY( 0 )

      IF nKey > 47 .AND. nKey < 58 

         ?? cDash

         DO CASE
            CASE nKey = 48
               ?? "zero"
            CASE nKey = 49
               ?? "one"
            CASE nKey = 50
               ?? "two"
            CASE nKey = 51
               ?? "three"
            CASE nKey = 52
               ?? "four"
            CASE nKey = 53
               ?? "five"
            CASE nKey = 54
               ?? "six"
            CASE nKey = 55
               ?? "seven"
            CASE nKey = 56
               ?? "eight"
            CASE nKey = 57
               ?? "nine"
         ENDCASE

         cDash := '-'

      ENDIF nKey > 47 .AND. nKey > 58 

   ENDDO

RETURN // Main()

IF..ENDIF

Control Structures – Decision making; IF..ENDIF

If Endif

/*

Control Structures - Decision making; IF..ENDIF

For more information refer here 

*/

PROCEDURE Main()
   CLS

   DO WHILE .T.
      ACCEPT "Enter first number : " TO cString1
      ACCEPT "Enter second Number : " TO cString2
      nNumber1 := VAL( cString1 )
      nNumber2 := VAL( cString2 ) 
      IF nNumber1 = 0 .AND. nNumber2 = 0
         EXIT
      ELSE 
         IF nNumber1 > nNumber2 
            ? nNumber1, "is greater than", nNumber2 
         ELSEIF nNumber1 < nNumber2 
            ? nNumber1, "is less than", nNumber2 
         ELSE
            ? nNumber1, "is equal to", nNumber2 
         ENDIF
         ?
      ENDIF
   ENDDO

RETURN // Main()

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()