Chapter 5 Programming Constructs

There are three programming constructs in any programming language:

Sequence
It is the order in which commands are executed.  Default sequence is the top to bottom, left to right.

Selection
Selection permits selective execution of commands, depending on whether a given condition is satisfied or not.  Syntax:

If <Condition>
<commands -- - - - --1>
else
<commands -- - - - --2>
end if

Where else part is optional and can be omitted.  Commands between if – end if will be executed only of condition is satisfied otherwise next statement is executed.

For every if there must be an end if.  Every end if is matched with the nearest unmatched if.  Example:

Set talk off
Set stat off
No1=0
Not2=0
@ 10,10 say “Enter No 1” get No1
@ 12,10 say “Enter No 2” get No2
read
if No1>No2
@ 15,10 say “No1 is Greater”
else
@ 15,10 say “No2 is Greater”
endif
wait

Do Case—End case
FoxPro evaluates case condition in the sequence they appear.  When case condition is found to be true.  Commands are executed and control moves to next statement following end case.  Hence only one of the command sequences will be executed at a time.  In a Do-Case many case conditions can be included.

Command sequence following otherwise will be executed only if none of the case conditions are true.  Otherwise and default command sequences are optional.  Every Do—case must have an Endcase.  For Example:
Set talk off
Store 1 to Mchoice
@ 10,10 get mchoice function ‘*Add; Mod; Delete’; Default1
Read
Do Case
Case Mchoice =1
@ 20,10 say “Add”
Case Mchoice =2
@ 20,10 say “Mod”
Case Mchoice =3
@ 20,10 say “Delete”
EndCase
Return

Iteration
If particular command sequence is to be repeated then an iteration construct is used.  Such constructs are called as Loops.  Syntax:
Do while <Conditon>
Commands - - - - - -
End do

If the condition is satisfied the loop is entered and commands are executed.  The commands are executed till the condition is true.  When condition becomes false the control exits the loop and executes next command following End do.  Initially if the condition is not satisfied the loop is skipped.  For Example:
Mnum=1
Do while Mnum <=10
? Mnum
Mnum = Mnum+1
Enddo

Scan – EndScan
Scan – Endscan loop specifically used for processing table files.  For Example:
            Use Flights
Do while .T.
Display
Skip
Enddo
Use

Do while loop does not terminate automatically when end of file is reached.  While Scan—EndScan loop automatically terminates when end of the file is reached.  Record pointer is automatically moved to next record in a Scan—EndScan loop so skip command is not specified.  For Example:
Use Flights
Scan
Display
EndScan
Use

Conditions can also be specified for a Scan—EndScan loop.  Only records which satisfy the condition will be processed.  For Example:
            Use Flights
Scan for Source= “Bombay”
Display
EndScan
Use

Exit command can be used within Scan—EndScan loop.  Which forces the control to Exit from the loop.

For Endfor
If number of times a loop is to be executed is known , For—EndFor Loop can be used.  For Example:
            For n = 1 to 10
            ? n
            Endfor

The File Maintenance Programs are
·         Add
·         Modify
·         Delete

Example 1:- Program to add a record in a database
set talk off
set safe off
use stu_marks
mmark1=0
mmark2=0
mmark3=0
mtotal=0
mpercent=0
@ 12,10 say "enter student id no:-"get mst_id          
@ 13,10 say "enter standard:-" get mstd
@ 14,10 say "enter hindi marks:-" get mmark1
@ 15,10 say "enter english marks:-" get mmark2
@ 16,10 say "enter marathi:-" get mmark3
read
mtotal=mmark1+mmark2+mmark3
mpercent=mtotal/3

appe blank
repl st_id with mst_id
repl std with mstd
repl mark1 with mmark1
repl mark2 with mmark2
repl mark3 with mmark3
repl total with mtotal
repl percent with mpercent

Assignments  (Add Records in a Database)
1)      Accept name, Designation, Dept, Salary and Calculate DA, TA, HRA PF, ESIC and NETSAL and add the record in Employee table.
2)      Accept Flight_code, Flight_Date, Source, Destination, Depart_Time, Depart_Date and add the record in the Flight Table.
3)      Accept Pass_Name, Passport_No, Flight_Code, Padd_Add, Tel_No, Destination, Fare and Add the record in the Passenger Table.
4)      Accept Salesman, Salesman_No, Area, Salary, Sales and calculate commission as if Sales is greater than Rs. 20000/- Comm = 5%, if sales is >15 and <20000 then Comm=4%, if sales is >10000 and <15000 then Comm = 3%.  And add the record in the Sales table.

Example 2:- Program to Modify Existing Record in a Database.
set talk off
set safe off
use stumark
index on st_id tag std
mst_id=0
mstd=0
mmark1=0
mmark2=0
mmark3=0
mtotal=0
mpercent=0
@ 10,10 say "the student id nomber to modify" get mst_id
read
seek mst_id

if not found()
@ 13,10 say "such no does not exit"
endif

if found()
mst_id=st_id
mstd=std
mmark1=mark1
mmark2=mark2
mmark3=mark3
mtotal=total
mpercent=percent
@ 15,10 say "the standard is:-" get mstd
@ 16,10 say "hindi is:-" get mmark1
@ 17,10 say "english is:-" get mmark2
@ 18,10 say "marthi is:-" get mmark3
read
mtotal=mmark1+mmark2+mmark3
mpercent=mtotal/3

repl st_id with mst_id
repl std with mstd
repl mark1 with mmark1
repl mark2 with mmark2
repl mark3 with mmark3
repl total with mtotal
repl percent with mpercent
repl result with mresult
endif

Assignments (Modifying Records in the table)
1)                  Write a program to modify records in a Flight Table.
2)                  Write a program to modify records in a Passenger Table.
3)                  Write a program to modify records in a Sales Table.
4)                  Write a program to modify records in a Employee Table.
5)                  Write a program to modify records in a Stu_Mark Table.

Assignments (If—End if Programs)
1)                  Accept a Color and display following messages for particular Color.
Black   =          “Sadness”
White  =          “Piece”
Yellow =          “Friendship”
Red     =          “Feelings”
Blue     =          “Vastness”
Else     “Wrong Color Entered”

2)                  Accept Name and Age and display the following messages for the Age entered with the name.
If Age >=20
“Adult and Qualified for Voting”
if Age<20
“Minor Disqualified For Voting”

Example 3:-Program to Delete A Record from the Database.
set talk off
set safe off
use student
index on st_id to stu1
mst_name=space(9)
mst_std=0
mst_id=0
mst_add=space(10)
mad_date={  /  /     }
mst_tele=0
mleave_date={  /  /     }
mdel='y'

@ 9,10 say "the student id to delet" get mst_id
read
seek mst_id

if not found()
@ 11,10 say "record is not exist"
endif

if found()
@ 13,10 say "the employee is:-"+(st_name)
@ 15,10 say "st_std is:-"+str(std)
@ 17,10 say "st_id is:-"+str(st_id)
@ 19 ,10 say "add_date is:-"+dtoc(date())
@ 21,10 say "leave_date is:-"+dtoc(date())
@ 27,25 say "do you wish to delet this record" get mdel
read
endif
if mdel='y'
delet
pack
endif

Assignments (Deleting Records in the table)
1)                  Write a program to Delete records in a Flight Table.
2)                  Write a program to Delete records in a Passenger Table.
3)                  Write a program to Delete records in a Sales Table.
4)                  Write a program to Delete records in a Employee Table.
5)                  Write a program to Delete records in a Student Table.

Counter Variable
Counter variable is that variable which counts the Numeric Value as per the User Requirement.  It can be declared as:
CTR = 0         
CTR = 1

Example 4: -Program to Display “Hello” Five Times
  set talk off
  ctr=1
  r=5
  c=5
  do while ctr<=5
  @ r,c say "hello”
  r=r+1
  ctr=ctr+1
  enddo 

Example 5:-Program to Accept 10 Numbers and to Display its Sum.
set talk off
no=0
sum=0
ctr=2
r=2
c=9
do while ctr<=10
@ r,c say "enter a no" get no
read
ctr=ctr+1
sum=sum+no
r=r+5
c=c+5
enddo
@ 20,10 say "The Sum is " +str(sum)

Example 6:- Program to display a Fibonacci Series.
Set talk off
A=0
B=1
C=0
Ctr=1
R=3
No=0
@ r,10 say “Enter a No” get No
read
do while ctr<=10
C=A+B
@ 22,2 say C
A=B
B=C
Ctr=Ctr+1
R=r+1
enddo