NOS : Certificate in Computer Applications
| Home | Table of Contents |
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.
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?
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
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:
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.
IN-TEXT QUESTIONS 2
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
23.10 FEEDBACK TO IN-TEXT QUESTIONS
IN-TEXT QUESTIONS 1
IN-TEXT QUESTIONS 2
.MEM
.VUE
.BAK2