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