Control Structures in Foxpro

Control Structures

IF statement
If Condition is True Executed and then False Not Executed.

Syntax
If (condition) then
Statement-1
End if

Example
clear
mark =0
@5,5 say " Enter the Mark : " get mark RANGE 0,100
Read
If mark >=40 Then
@10,10 say " You have PASS"
Endif

If….else….endif:-
The commands between if…..end if will be executed only if condition is satisfied, otherwise the next statement is executed. For every if there must be an end if. Every is matched with the nearest unmatched if.
Syntax:-
If<condition>
<Command sequence-1>
Else
<Command sequence-2>
End if

Command sequence –1 will be executed if a condition is true, if condition is false command sequence-2 will be executed. Control falls to the next statement in either case, if program I still in execution.

Example 2
clear
Store 0 to x,y
@5, 5 say " Enter the First value: " get x
@7,5 say " Enter the Second value: " get y
Read
IF x > y Then
@10, 10 say str(x) + “is Greater than” + ltrim (str(y))
Else
@10, 10 say str(x) + " is lesser than " + ltrim (str(y))
Endif

Example 3
clear
Store space(1) to x,ch
@5, 5 say " Enter any Alphabet: " get x
Read
ch =chr(asc(x) +32) && To convert Upper into Lower
IF ch="a" .or. ch="e" .or. ch="i" .or. ch="o" .or. ch="u" then
@10,10 say ch + " is a VOWEL "
Else
@10,10 say ch + " is a CONSONANT"
Endif

 

NESTED IF: (IF within IF)
clear
store 0 to x,y,z
@5,5 say " Enter No1 : " get x
@7,5 say " Enter No2 : " get y
@9,5 say " Enter No3 : " get z
Read
If x > y then
IF x>z then
@ 15,5 say " X is Greater than y and z"
Else
@15,5 say "X is Greater than Y and Lesser than Z"
Endif
Else
IF Y>Z then
@ 15,5 say " Y is Greater than X and Z"
Else
@15,5 say "Y is Greater than X but not Z"
Endif
Endif

DO CASE
Case Commands are used to check for a specified condition

Syntax:
DO Case
Case <variable> = <value>
Statement -1
Case <variable> = <value>
Statement -2
Otheriwse
Statement -3
End Case

Example:
clear
store 0 to day
@5,5 say " Enter any number from 1 to 7 " get day
Read
            DO CASE
                        case day = 1
                        @10,10 say  "SUNDAY"      
                        case day = 2
                        @10,10 say  "MONDAY"
                        case day = 3
                        @10,10 say  "TUESDAY"
                        case day = 4
                        @10,10 say  "WEDNESDAY"
                        case day = 5
                        @10,10 say  "THURSDAY"
                        case day = 6
                        @10,10 say  "FRIDAY"
                        case day = 7
                        @10,10 say  "SATURDAY"
                        OTHERWISE
                        @10,10 SAy "Invalid Input"

           

EndCase

FOR LOOP

  • To repeatedly execute a series of lines in a Program.
  • The lines of code b/w FOR and ENDOFR will be executed until the memory         variable is equal to the final condition specified.
  • Default STEP value is 1.

Syntax
FOR <memvar> = <initial value> TO <final value> STEP <no>
................
................
ENDFOR

Example: 1

CLEAR
FOR I = 1 TO 10
? I
EndFor

Example: 2
            To print the EVEN nos from 2 to 50
CLEAR
FOR I = 2 TO 50 STEP 2
? I
EndFor

Recommended :

FoxPro e-Book
  1. FoxPro Programming Quick Reference e-Book (New! Revised Edition)