NOS : Certificate in Computer Applications
| Home | Table of Contents |


Up: Online Course Material


LESSON 23

PROGRAMMING CONCEPT

23.1 INTRODUCTION

By now we are familiar with various dBASE commands and how they are used. Let us consider another use of dBASE namely in the area of programming. In this lesson we will discuss some programming constructions in general sense. We will also point out the situations in which they are used.

We had given a brief idea about the concept of programming while dealing with the lesson on 'Software' in the module 'Computer Fundamentals'. Here we will discuss programming in the context of dBASE III Plus.

23.2 OBJECTIVES

After going through this lesson you should be in a position to

23.3 WHAT IS THE NEED OF PROGRAMS?

In the previous three lessons you have learnt so many commands in dBASE. These commands are given to the computer step by step. We give a second command only after the first command is executed. But many times the user has to give a set of instructions repeatedly to solve a particular problem. In such situations writing the same set of instructions every time is time consuming. Another factor is the chance of committing mistakes while keying in the commands. Hence we need to develop computer programs and store it in a file.

As you know program is a set of instructions given by the user to the computer for solving a problem. In the programming mode it is possible to write any number of commands together in single file. When we give a single dBASE command to execute the file all the dBASE commands contained in the file are executed.

Now let us discuss some of the important commands that are used in programming. They can be broadly categorised into three areas: Control Commands, Input Commands and Output Commands. Within these categories we see a number of commands. We will discuss the important ones only in this lesson.

23.4 CONTROL COMMANDS

The important commands under this heading are

We will now consider the general forms of these commands.

23.4.1 DO WHILE COMMAND

This command is used when a particular segment of commands have to be executed repeatedly. Thus it is a looping command that sustains operation while some condition is true. The loop command is used to return control directly back to the DO WHILE command. The first line of the DO WHILE statements has the form:

DO WHILE logical expression

Continuous operation is sustained as long as the expression is true, or until an ENDDO or EXIT command is encountered. The complete DO WHILE statement has the form:

DO WHILE expression

(Command lines)

ENDDO

Now let us consider an example using the do while command. Suppose you have to fine the sum of natural numbers 1 to 100. (That is 1+2+3+4+………..+100). In order to do this we take two variables S and N. S is the sum of the numbers. N stands for natural number which takes value from 1 to 100.

S=0

N=1

DO WHILE N<101

S=S+N

N=N+1

ENDDO

 

In the above example we assume that S=0 and N=1. Let us interpret the command line N=N+1. This shows that natural numbers increase by 1 at a time. The command line S=S+N shows the sum of numbers.

When N=1, obviously the sum of numbers will be S=S+N = 0+1= 1.

When N=2, S= 1+2=3.

When N=3, S=3+3=6

…………

and so on.

Here the role of the DO WHILE command is that, it repeats the summation process till N<101. Every time a sum is obtained by the statement S=S+N, N takes the next value N=N+1. Thus a loop is formed. The process is repeated till the expression N<101 is true. The process stops at the ENDDO command.

23.4.2 EXIT COMMAND

The exit command transfers control from the DO WHILE loop to the command line following the ENDDO statement, which is always the last line in a DO WHILE statement. Let us consider the following example:

Do while N < 51

S = S + N

If N = 10

Exit

Endif

Enddo

The exit statement completely exits the control from the loop when the value of N = 10 and immediately it stops execution following the ENDDO statement.

23.4.3 The IF…ELSE…ENDIF Commands

The IF…ELSE…ENDIF clauses allows a program to make decesions while it is running. The IF command means the same thing that it does in English: IF this is the case THEN do this ELSE do not instead.

IF <condition>

<commands…>

[ELSE]

<commands…>

ENDIF

Where <condition> is a valid dBASE logical expression, ELSE is an optional branch to alternative commands, and ENDIF is the command required to close the IF clause. (If you forgot to include the ENDIF command, dBASE will not give you any error message. Instead, your program will just behave in a strange and unpredictable manner.)

23.4.4 LOOP COMMAND

When the loop command is executed the control passes back to the command line following the DO WHILE command. Sometimes it may be required to stop executing some of the statements at the lower end of the loop body and transfer the control to the beginning of the loop. In that case the loop statement is used as shown below:

Do while logical-expression

………………. (command lines)

………………..

If condition

Loop

Endif

…………….. (command lines)

……………..

Enddo

23.4.5 EOF( ) Command

The EOF statement, which stands for end of file, is used with several commands including DO WHILE, DO CASE, and IF (We will discuss these commands later). When an end-of-file condition exists EOF( ) returns a true value. Otherwise it shows a false value. Going to the bottom of a file with GO BOTTOM does not position the record pointer to the end of the file. Let us consider the following example

.USE FILENAME

.GO BOTTOM

. ? EOF( )

.F

You can also go to the end of the file with the COUNT command.

23.4.6 DO CASE , ENDCASE, AND OTHERWISE COMMANDS

The DO CASE Command is used when only one option out of several alternatives (That is several CASEs) is to be selected. The CASE statement is used to check for a specific condition. Each CASE statement is followed by one or more command lines. If a CASE statement is true, dBASE performs all commands between that CASE statement and the next CASE statement. If a CASE statement is false the command lines are ignored and the control passes to the next CASE statement. This process continues till a true CASE is found or the ENDCASE statement is reached.

DO CASE

CASE X = 1

………………

………………. (command lines)

CASE X = 2

………………..

……………….. (command lines)

CASE……….

ENDCASE

Let us consider the following example.

X = 5

DO CASE

CASE X < 10

? "X is less than ten"

CASE X = 5

? "X is equal to five"

ENDCASE

However, the output from this routine is simply X is less than ten. Although it is true that X is equal to five, the second CASE statement is never evaluated. In a DO CASE clause, only the first alternative that evaluates to true is processed; all remaining commands and CASE statements up to the ENDCASE command are ignored completely. In other words, the alternatives in the DO CASE clause are mutually exclusive.

OTHWERWISE: It is an alternative statement, which is used when none of the CASE statements are true.


Top

IN-TEXT QUESTIONS 1

1. Give three examples of control commands.

2. What is the usefulness of EXIT command?

3. When do you use the DO CASE command?

 


Top

23.5 DATA INPUT COMMANDS IN dBASE

Now we will discuss some input commands, which are used to get data into a dBASE program.

23.5.1 ACCEPT Command

The ACCEPT command presents a prompt on the screen and waits for the user to respond by typing some text and pressing ENTER key. Whatever you type is stored in a memory variable.

The syntax for the ACCEPT command is:

ACCEPT [<prompt>] TO <memory variable>

Where <prompt> is optional and <memory variable> is the name of the character memory variable that will store the user's entry.

The data stored in the memory variable can be from 1 to 254 characters in length. If the user presses the ENTER key without typing any character, the stored memory variable has an ASCII value of zero and a length of zero.

PROGRAMMING THE PROMPT

The optional prompt can be surrounded by quotation marks, apostrophes, or brackets. The examples below are equivalent:

ACCEPT "Enter your name" TO Name

ACCEPT 'Enter your name' TO Name

ACCEPT [Enter your name] TO Name

To embed an apostrophe or other character in the prompt, just use different characters for the outside delimiters:

ACCEPT "What's your name" TO Name

The prompt itself can be stored in a memory variable.

Characteristics of ACCEPT Command

  1. Any data entered via the ACCEPT command can be used for macro substitution.
  2. ACCEPT will work with character strings of any length up to 254 characters.
  3. To restrict the size of the memory variable character string, use the @….READ command.
  4. To convert the character data entered via the ACCEPT command to numeric or date data, use the VAL or CTOD functions.

23.5.2 INPUT COMMANDS

INPUT command is usually used to enter numeric data from the user. The optional prompt can be enclosed in double quotation marks, apostrophes, or brackets.

The syntax for the INPUT command is:

INPUT [< prompt >] TO < memory variable >

Where <prompt> is an optional character string enclosed in double or single quotation marks or brackets and <memory variable> is any valid memory variable name.

INPUT "Enter your age" TO Age

INPUT ‘Enter your age’ TO Age

INPUT [Enter your age] TO Age

INPUT [Enter your age] TO Age

Let us consider another example to get data into the program through INPUT command.

INPUT "Enter value for the principal" TO principal

The computer will display the prompt on the screen and will wait until you enter the value for the principal variable.

23.5.3 WAIT COMMAND

The WAIT command suspends processing of a program and waits for the user to press any key. It accepts only a single keystroke and does not require the ENTER key to be pressed.

23. 5.4 @ ROW, COLUMN COMMAND

The symbol @ followed by row and column position used to position text at a specific row and column location on the screen. The row-column notation starts at 0,0 which is the first row and first column on the screen. Therefore, rows are 0-24 for a 25 line screen and columns are 0-79 for a 80 column screen. You can think of @ row,col as ‘at row number, column number’. The SAY and GET statements are used individually or in combination with @ row,col. Let us consider the following example:

  1. @ 7,10 SAY "What is your name?"
  2. @ 7,40 GET name
  3. @11,10 SAY "PIN number?"
  4. @11,40 GET PIN number
  5. READ

In this example, line 1 displays the question "What is your name?’ at row 7, column 10 on the screen. Line 2 allows data entry from the keyboard into name field of the database in use. Line 3 and line 4 are similar to lines 1 and 2. Line 5 contains READ command which reads your typed response to the GET statement into the specified field of the database in use.

23.6 OUTPUT COMMANDS

These are some of the important commands used in dBASE in the programming mode in addition to all dot prompt commands we have used earlier. Examples are APPEND, BROWSE, EDIT, LIST, etc.

23.7 dBASE COMMANDS AND FUNCTIONS

Several different types of files are created and used by dBASE III Plus. Let us discuss some important files and their extensions. They are:

Backup files

.BAK

Catalog files

.CAT

dBASE configuration file

CONFIG.DB

System configuration file

CONFIG.SYS

Database files

.DBF

Database memo field file

.FRM

Report form files

.FRM

Label form files

.LBL

Memory variable files

.MEM

Database index files

.NDX

Command files

.PRG

Query files

.QRY

Screen files

.SCR

Standard data (or text) files

.TXT

View files

.VUE

When the files are created by dBASE, they are assigned different three-character extensions, like CUSTOMER.DBF. Other files, like text files and report form files are created by dBASE. We give a description for each file type listed above.

  1. Backup (.BAK): Backup files are automatically created as a safety measure when an edited version of a text file is saved. For example, if you use the MODIFY COMMAND FILENAME statement to change a file, the previous version of the modified file is kept with a .BAK extension. This happens when you press CTRL-W to save (write) your modified version.
  2. Catalog (.CAT): A catalog of filenames, including databases, index Files, etc. is created as files are created or put into use. The catalog file is established with SET CATALOG TO filename before file creation activity is started.
  3. dBASE Config (CONFIG.DB): This file is used by dBASE when it is started. It contains one or more commands that control dBASE operation. Normal defaults, including function key values and SET functions, are controlled by this file. It can also contain a command line that causes automatic execution of a command file.
  4. System Config (CONFIG.SYS): This file is used by DOS when you turn on your computer. It lets you have 20 files open at the same time, including 15 within dBASE III applications. It also creates 15 buffers, which speeds up dBASE III operation by letting it work in memory buffers rather than having to read and write information to your disk during sorting and listing.
  5. Database (.DBF): A standard database file is created and saved using te CREATE command.
  6. Memo file (.DBT): When a database file contains memo (text) fields, an auxiliary file having the extension .DBT is created to contain the memo fields . Databases can contain as many as 128 memo fields. Each memo field can contain up to 5,000 characters. Although memo fields use a minimum of 512 bytes in the .DBT file, they only occupy 10 bytes in a database file. Memo files are accessed when a database is being edited with APPEND, BROWSE, CHANGE, or EDIT by pressing Ctrl-Home at the begining typing text into a memo field, press Ctrl-End to return to the data entry mask.
  7. Report form (.FRM): The CREATE/MODIFY REPORT command helps you to create and save report format files that control the display and printing of data. The REPORT commands are described in lesson 22.
  8. Label from (.LBL): The CREATE/MODIFY LABEL command helps you create and save label formats files that control the display and printing of data.
  9. Memory Varible (.MEM ) Memory files are created when memory variables are written to disk using the SAVE command. The resulting file has the extension .MEM which allows you to save memory variables to disk, and then clear memory to make room for more memory variables.
  10. Index (.NDX) An index file is created from an existing database file. The records within an index file are sorted (rearranged alphabetically or numerically) on one or more specified fields, which are refereed to as "key" fields. Records retain their original record number. Changes to the contents of index files change the database file.
  11. Procedure (or command file) (.PRG) A procedure or command file is created and saved using the dBASE full-screen editor or some other text editor; These are pure ASCII files and are transferable (transportable) between different computer and operating systems. Command files are created by the dBASE MODIFY COMMAND.
  12. Query (.QRY): A query file establishes a filter that restricts display to specific records that meet some established condition. The CREATE/MODIFY QUERY command is used.
  13. Screen (.SCR): A screen file is used to create or edit screen format files. The CREATE/MODIFY SCREEN command is used.
  14. Standard Data (.TXT): A standard data or text file is created by copying a database file with COPY TO filename SDF or DELIMITED. .TXT file is created when information is displayed when the SET ALTERNATE TO filename and SET ALTERNATE ON commands are in effect. The resulting file may be used by other programs, such as WordStar’s Mail Merge.
  15. View (.VUE): Contains the name of related database files and related index and format files, which list selected field names and relations. Used to call all related files with a single command. The CREATE/MODIFY VIEW command is used for this purpose.

 


Top

IN-TEXT QUESTIONS 2

  1. What is the difference between INPUT and ACCEPT commands.
  2. Give the extension of the following database files:

Program File

Memo File

View File

Back up File

23.8 WHAT YOU HAVE LEARNT

In this lesson we discussed the need for programming in dBASE. In this context we discussed three types of programming commands. These are control commands, input commands and output commands. The important commands available and their syntax for use in practice are discussed. We have provided examples so that you can develop programs in dBASE yourself.

23.9 TERMINAL QUESTIONS

  1. Write a program for simple and compound interest using DO-WHILE command.
  2. Write a program for finding the sum of natural numbers between 1 to 10.
  3. Write a program for printing Pay -slips using INPUT and ACCEPT commands.
  4. Write a program for finding prime numbers between 1 to 100.

23.10 FEEDBACK TO IN-TEXT QUESTIONS

IN-TEXT QUESTIONS 1

  1. Do While, Exit, If … Else…Endif
  2. The exit command transfers control from the DO WHILE loop to the command line.
  3. The DO CASE Command is used when only one option out of several alternatives (That is several CASEs) is to be selected.

IN-TEXT QUESTIONS 2

  1. INPUT command is usually used to enter numeric string whereas ACCEPT command is used in character string.
  2. .PRG

.MEM

.VUE

.BAK2


Top