Jonathan E. Sisk's
Pick/BASIC: A Programmer's Guide
Chapter 3 - Fundamental Statements and Instructions
In the first program, many of the most fundamental principles of programming in PICK/BASIC are discussed. Topics, statements, and functions covered include comments (remarks), PROMPT, PRINT, INPUT, ABS, SQRT, ALPHA, NUM, END, IF-THEN, STOP, the null string, and LEN.
Enter the program in Fig. 3-1. An explanation of each instruction and technique follows. Note that most of the spaces in the tutorial programs are put there for visual esthetics. The easier a program is to read, the easier it is to maintain. Generally, spaces are optional, but there are some cases where they are not. To be safe, enter the programs exactly as shown in the examples.
Fig. 3-1. Program Example 1.
>ED BP EX.001 TOP .I<cr> 001 * EX.001 002 * BASIC TERMINAL I/O AND CONDITIONAL EXPRESSIONS 003 * mm/dd/yy: date last modified 004 * JES: author's initials 005 * 006 PROMPT ":" 007 PRINT 008 * 009 * GET NAME 010 * 011 PRINT "PLEASE ENTER YOUR NAME " : 012 INPUT NAME 013 PRINT 014 * 015 * CHECK TO SEE IF THEY WANT TO STOP 016 * 017 IF NAME = "" OR NAME = "QUIT" THEN STOP 018 PRINT "HELLO THERE " : NAME 019 PRINT 020 * 021 * NOW GET STRING OR NUMBER 022 * 023 PRINT "PLEASE ENTER A NUMBER OR A STRING OF LETTERS " : 024 INPUT RESPONSE 025 IF RESPONSE = "" OR RESPONSE = "QUIT" THEN STOP 026 PRINT 027 * 028 * CHECK TO SEE IF STRING IS NUMERIC 029 * 030 IF NUM(RESPONSE) THEN 031 PRINT "ABS VALUE OF " : RESPONSE : " IS " : ABS(RESPONSE) 032 PRINT "THE SQUARE IS " : RESPONSE * RESPONSE 033 PRINT "THE SQUARE ROOT IS " : SQRT(RESPONSE) 034 STOP 035 END 036 * 037 * CHECK TO SEE IF STRING IS LETTERS ONLY 038 * 039 IF ALPHA(RESPONSE) THEN 040 PRINT "THE LENGTH OF " : RESPONSE : " IS " : LEN(RESPONSE) 041 STOP 042 END 043 * 044 * STRING MUST CONTAIN NON-ALPHA AND NON-NUMERIC CHARACTERS 045 * 046 PRINT "YOUR RESPONSE WAS NOT CONSIDERED NUMERIC OR ALPHABETIC" 047 END 048 <cr> .FI
Having entered the program, you are now ready to compile and run it. From now on, after entering each program example, compile it with the BASIC verb, catalog it with the CATALOG verb, then type the program name at the TCL prompt. Once again, here's an example:
>BASIC BP EX.001<cr> *********************************************** SUCCESSFUL COMPILE! 1 FRAMES USED. >CATALOG BP EX.001<cr> 'EX.001' CATALOGED. >EX.001<cr>
Once the program loads and begins execution the following prompt appears on the screen:
PLEASE ENTER YOUR NAME:
A feature of this book is the detailed explanation of each new topic, or derivatives of earlier topics, following the tutorial programs. The instruction, or group of statements and instructions, are repeated where the narrative explains them. This technique minimizes the amount of backtracking that you will have to do in order to examine the actual source instructions.
A note for FORTRAN and COBOL programmers learning Pick. After a quick visual scan of the program EX.001, or any other PICK/BASIC program, you will notice the total absence of data typing via implicit assumptions by virtue of the first character in the data/field names (i.e., variables beginning with I through N do not imply integer data). Nor do you find the ever-present "DATA DIVISION" that all COBOL programs must contain. Remember that no data typing exists in the Pick System. A field is a field is a field.
PROGRAM NAMES AND COMMENT SECTIONS
The first five lines of Programming Example 1 consist of comments:
001 * EX.001 002 * BASIC TERMINAL I/O AND CONDITIONAL EXPRESSIONS 003 * mm/dd/yy: date last modified 004 * JES: author's initials 005 *
The Pick System is very flexible regarding the manner in which files and items are named. This usually results in some rather diverse item-ids, particularly when naming programs. (A program is stored as an item in the Pick System, so a program name is an item-id). For instance, a programmer may decide to call a program "AP101," which may mean to him that this is the program to enter and update the accounts payable vendor file. Others go to the extreme of identifying the nature of the program in its item-id. For instance, another programmer may call this same program "ENTER.ACCOUNTS.PAYABLE.VENDOR."
Rather than trying to explain the entire program in the program name, it may be more useful to decide on a simple item-id and explain the purpose of the program through remarks in the program. A comment section is simply a standard set of remark statements that appears at the beginning of each program. Remark statements have little effect on the execution of the program. This technique makes it easier to work with program files.
The Program Name
It's usually a good idea to use line 1 of each program to repeat the program name:
001 * EX.001
The reason for this is that programs which are considered external subroutines require a SUBROUTINE statement on this line, so you can't always count on this line being available. Note: Instead of placing a remark on line one, it is permissible to put the word "PROGRAM," followed by the program name. This little-known feature has been present for years in the Pick System.
The Program Description
Line two contains a single-line description of the program:
002 * BASIC TERMINAL I/0 AND CONDITIONAL EXPRESSIONS
Other examples could be "UPDATE THE ACCOUNTS PAYABLE VENDOR MASTER FILE," or "EXTRACT CUSTOMER LISTS FROM DATABASE,"
Date Last Modified and Author's Initials
The "date last modified" is the last date the program was changed:
003 * mm/dd/yy: date last modified
Follow a consistent date format, like mm-dd-yy, for example:
003 * 12/15/97: date last modified
The comment on line 4 gives the initials of the last person who changed the program, which is useful for finding and persecuting the guilty party.
004 * JES: author's initials
"Spacer" Remark Lines
Line 5 is a "spacer remark line," which provides a visual separation of the comment section from the actual executable code of the program:
This treatment is for visual aesthetics only and has no impact on the actual execution of the program; it just makes the program look more organized for the benefit of the programmer. Once again, the easier code is to read, the easier it is to maintain.
THE PROMPT STATEMENT
The PROMPT statement defines the character to display on the screen when an INPUT statement is executed:
006 PROMPT ":"
Any expression which produces a single character may be used as the argument. When this statement is left out of the program, the default prompt character is a question mark.
THE PRINT STATEMENT AND BLANK LINES
The PRINT statement is used to print data on the screen or printer. All by itself on a line, it simply skips a line on the output device:
When followed by an expression, the current value of the expression is displayed or printed. Chapter 15, Example 13, discusses routing output to the system printer with the PRINTER ON statement.
ANNOTATING THE PROGRAM
Comments, also culled remarks, have been discussed at several points already throughout this book. The block of comments in lines 8-10 serves two purposes:
008 * 009 * GET NAME 010 *
First, it provides the visual spacing to make the source code more readable to those who must maintain it and, second, the comment on line 9 reminds the programmer about the task at hand.
THE PRINCIPLE OF PSEUDOCODE
One important principle of program design is "pseudocode." "Pseudocode" is the process of describing program logic in plain English. Since a program is simply a set of instructions to perform a task, it is often necessary to plan out the logic of the problem before starting to write the code that will accomplish it. This may be done by organizing the logic in plain English statements, in the form of remarks. This way, the logic may be debugged before even one line of executable source code is ever written.
For example, if you wanted to teach a robot how to "make toast," you have to tell it the steps to perform. For instance, the robot would have to go to the kitchen, find the bread, remove a slice, find the toaster, put the bread in the toaster, push the button, wait for it to cook, remove the bread, and then find a plate to place it on. Once this sequence of events has been "programmed," all you need to tell the robot the next time you want mast is simply "make toast." The same principle applies to telling the robot how to make coffee, eggs, juice, etc. After the robot has been taught all of the individual routines, you may then put them all together by telling the robot to "make breakfast." To the robot, this means make toast, make coffee, make eggs, etc. Although this may seem like an elementary example, it introduces the principle of modularity. Programs typically are comprised of a series of individual, self-standing modules, each of which performs a specific task.
As this relates to the subject at hand, the entire logic of any program problem may be written first in comments, as in this example. Once the logic is in place, the source instructions of the appropriate language may be inserted after the comments, hence the principle of pseudocode.
MORE ON THE PRINT STATEMENT
The PRINT statement in line 11 displays "PLEASE ENTER YOUR NAME" on the screen and positions the cursor two spaces to the right of the word NAME. Note that the space between the word NAME and the "character is to force the space between the literal and the cursor.
011 PRINT "PLEASE ENTER YOUR NAME " :
Throughout this book, many other forms of the PRINT statement are explained, including its ability to print at specific coordinates on the screen or to do special functions like erase the screen. The PRINTER ON and PRINTER OFF statements also affect the output of PRINT statements.
THE INPUT STATEMENT
The INPUT statement causes the program to wait for the user to enter something on the keyboard:
012 INPUT NAME
When input is received, it is placed into the variable following the INPUT statement, which in this case is the variable NAME. Generally, what the user enters on the keyboard must be followed by a press of the Return key, although Pick provides the ability to automatically issue the Return when a certain number of characters have been received.
THE IF STATEMENT AND NULL STRINGS
The IF statement is used to determine whether or not a certain condition applies before continuing execution. IF statements are always followed by conditional expressions, which are expressions which derive either a "true" or "false" value. In the Pick System, "true" is represented by a numeric nonzero (normally 1), and "false" is represented by 0 (zero) or null.
017 IF NAME = "" OR NAME = "QUIT" THEN STOP
This IF statement checks to see if the variable NAME contains any data. The portion of the statement NAME = "" reads like this: If the NAME is null, which means that you simply pressed the Return key, then the conditional expression is true; if you entered a < cr >, then the variable NAME will be cleared (set to null). In PICK/BASIC, the null string is represented as '' (two single quotes) or "" (two double quotes) or even "\\" on some platforms. . The ":" prompt character previously defined in the PROMPT statement appears to the immediate left of the cursor.
IF ANSWER = "Y" THEN ... IF AMOUNT > 0 THEN ... IF OLD.AMOUNT # NEW.AMOUNT THEN ...
Note: The situations where the equivalence to 0 (zero) of a null value, which is typically represented by two quotation marks with nothing between them, occurs in other places in the Pick System. For example, if you attempt to add up fields which normally contain numeric data, such as money amounts, and one or more fields contain null (or any other non-numeric data), then these fields are treated as though they contained the value 0 (zero).
A good way to check how your system handles nulls is to try the following statements in a test program:
001 A = "" 002 B = 0 003 IF A = B THEN PRINT "YES" ELSE PRINT "NO"
On most versions of Pick, this will print "NO"
Two or more conditional expressions may be connected by a Boolean operator, which is one of the words AND or OR. Normally, when only one conditional expression follows an IF instruction, then the statement (or statements) following the THEN is (are) executed if the expression evaluates true. Otherwise, unless an ELSE is present, control "falls through" to the next physical line of the program. The ELSE initiator is optional in the IF construct, and is explained at the end of Example 2 in Chapter 4.
When two conditional expressions are connected with an OR, then either of them evaluating true causes the statement(s) following the THEN initiator to be executed. Having an AND connect the conditional expressions means that both expressions must evaluate true in order for the statement(s) following the THEN initiator to be executed.
This line has two possible choices for executing the STOP statement which follows the THEN initiator. Either a null (carriage return or line feed) or the word QUIT would have caused the STOP statement to be executed.
Here are sample conditional expressions using Boolean operators:
IF ANSWER = "Y" OR ANSWER = "" THEN ...
This statement indicates that if either of the conditional expressions evaluate to true, then the statement (or statements) after the THEN initiator will be executed.
IF BALANCE.DUE > 0 AND INTEREST.FLAG = "Y" THEN ...
This second statement indicates that both of the conditional expressions must evaluate true in order to execute the statement (or statements) following the THEN initiator.
BAIL-OUT MECHANISMS: THE CONCEPT OF "ESCAPE PODS"
Eventually (if it hasn't happened to you already) you will run a program that prompts you to enter something and you will not know exactly what it wants. Maybe it needs something in a particular format, like a date or money amount, but you have no way of knowing. So you try pressing Return and it displays something like "INPUT REQUIRED!" and prompts you again. You try "END" or "QUIT" or "?" or "HELP." Then you try your favorite obscenity, again to no avail. In frustration, you try the data entry operator's last resort--the BREAK key--and find that it has been disabled. At times like this you reconsider your future in data processing and consider a less frustrating career, like religious cult management or being an air traffic controller.
To make a long story short, it is a very thoughtful touch to allow operators an escape or "bail-out" mechanism. This means that any time in your programs that an operator is prompted to input data, one consistent response is always available. This word (or character) is their panic button. In all of the tutorial examples, QUIT is used as the magic word. You may choose anything you want, but whatever you choose should be the first thing you teach your data entry operators. Tell them that when they enter this word, it means "Get me out of here!"
Implementing the escape pod principle has some remarkable side effects. First, it greatly reduces the fears of your users, assuring them that they may always escape in an orderly fashion from any program without hurting anything. Second, it allows you, the programmer, the benefit of cleaning up any unfinished program business before returning the user to the process or menu that sent them there.
The most important aspect of implementing your escape pod scheme is that you remain consistent in what you check or test for to allow them to bail out. Don't make it "X" in one place, "QUIT" in another, and "END" in yet another.
Line 17 is waiting for either a null or "QUIT" to allow the operator to get out. If that's what the operator inputs, then the program stops.
THE STOP STATEMENT
The STOP statement immediately terminates the execution of a program:
017 IF NAME = "" OR NAME = "QUIT" THEN STOP
If the program was activated from TCL, by entering >RUN BP EX.001, or >EX.001, then control returns to the TCL prompt character. If, however, the program had been executed from a menu, perhaps written in the PROC (procedure) language, then control automatically returns to the menu. The ABORT statement is the first cousin to the STOP statement. The difference is that when the ABORT is executed, control returns unconditionally to TCL, regardless of how the program was activated.
PRINTING EXPRESSIONS WITH PRINT
This statement prints the literal "HELLO THERE "on the screen and prints the current value of NAME, meaning, whatever you entered previously at the prompt to enter your name. Note that there is a space after the word "THERE" to force a space between the word "THERE" and your name.
018 PRINT "HELLO THERE " : NAME
This print expression was composed by a concatenation operation. First the literal "HELLO THERE "was concatenated (or linked) to the current value of the NAME variable. Once the concatenation was complete, the entire expression was printed.
Avoiding Redundancy and Repetition. Many of the instructions that have been covered up until now, such as PRINT, INPUT, IF-THEN, PROMPT, and comments, are used extensively throughout the rest of the tutorials. Rather than explaining the same things over and over, they will not be documented hereafter unless some new twist or nuance is being introduced.
THE NUM FUNCTION
The NUM function is one of several Pick intrinsic functions considered to be a "self-standing" conditional expression, i.e., one which evaluates to either a zero or null for "false," or a numeric nonzero value (normally 1) for "true."
030 IF NUM(RESPONSE) THEN 031 PRINT "ABS VALUE OF " : RESPONSE : " IS " : ABS(RESPONSE) 032 PRINT "THE SQUARE IS " : RESPONSE * RESPONSE 033 PRINT "THE SQUARE ROOT IS ": SQRT(RESPONSE) 034 STOP 035 END
This statement tests the string received in RESPONSE to determine whether or not it is a number. In the Pick System, a number is a string of digits with an optional period or decimal point. If it is determined to be a number, then all of the statements, up to and including line 34, are executed. If the response is not a number, then execution continues after the next END statement (which occurs on line 35).
An important note: Some, but not all, versions of the Pick System consider "null" to be numeric. This adds a level of complexity to your IF statements when they determine whether something is numeric; it must additionally be checked to see if it is null or not null, as the case may be.
If you did enter a number, then the statements on lines 31-35 are executed.
THE ABS FUNCTION
The ABS function produces the absolute (positive) value of a numeric string:
031 PRINT "ABS VALUE OF " : RESPONSE : " IS " : ABS(RESPONSE)
Suppose that you had entered -123.45 as your response; Line 31 would display:
ABS VALUE OF -123.45 IS 123
The ABS function comes in handy when printing the bottom-line totals of profit and loss reports for unstable companies.
Note that no range checking took place on the numbers you entered. This program may produce unusual results if the number is too large.
FINDING SQUARES AND SQUARE ROOTS
The next two lines utilize the PRINT statement and the SQRT function to determine squares and square roots.
032 PRINT "THE SQUARE IS " : RESPONSE * RESPONSE 033 PRINT "THE SQUARE ROOT IS " : SQRT(RESPONSE)
The statement in line 32 prints the message "THE SQUARE IS ", followed by an expression which takes the value of RESPONSE and multiplies it by itself. This is here simply to demonstrate the fact that formulas may be 1) performed in the same line as a PRINT statement, or 2) may be "assigned" to a variable on a separate source line and have the value displayed here. Both ways am discussed throughout this book.
There is also the intrinsic function EXP, which may be used to raise a number to other exponential values, as well as the ^ operator that also indicates exponentiation. See Appendix B for the syntax.
The SQRT function produces the square root of a numeric expression. This displays the message shown, followed by the square root of RESPONSE.
THE END STATEMENT
When an IF-THEN statement or IF-THEN-ELSE statement spans more than one physical line of a program, it must be terminated with an END statement:
This principle is referred to as the initiator/terminator relationship, and is discussed immediately following this example. This particular END statement terminated the THEN initiator which started at line 30.
THE ALPHA FUNCTION
The ALPHA function, like the NUM function discussed earlier, is also considered a conditional expression, one which evaluates to a true (numeric nonzero) or false (zero or null) condition. They are most often used in IF- THEN, IF-THEN-ELSE, and in CASE constructs:
039 IF ALPHA(RESPONSE) THEN 040 PRINT "THE LENGTH OF " : RESPONSE : " IS " : LEN(RESPONSE) 041 STOP 042 END
This statement tests the variable RESPONSE to determine if it contains only alphabetic characters. This means no characters other than the letters A through Z (upper- or lowercase). If this evaluates true, then the statements up to the next END statement are executed. Otherwise, execution of the program continues after the next END statement, which happens to occur on line 42.
If this message appears, then it means that the response that you provided to the variable RESPONSE was not considered either entirely numeric or alphabetic:
046 PRINT "YOUR RESPONSE WAS NOT CONSIDERED NUMERIC OR ALPHABETIC"
Consequently, this line displays the message shown and program execution stops at the next line.
THE LEN FUNCTION
If you enter a string of characters that is considered to be composed entirely of alphabetic characters at the INPUT statement on line 24, then the LEN statement on line 40 will be executed:
040 PRINT "THE LENGTH OF " : RESPONSE : " IS " : LEN(RESPONSE)
The LEN function determines and reports the number of characters in a string or string expression. Suppose you enter "ARISTOTLE" into the variable RESPONSE. Line 40 then displays
THE LENGTH OF ARISTOTLE IS 9
THE FINAL END STATEMENT
The END statement (more accurately called a compiler directive) occurs in two places in PICK/BASIC. The first occurrence is when it is used as a terminator for a multiline IF-THEN or IF-THEN-ELSE statement. The second and final form is as a program terminator, meaning that this is the logical end of the program. On most Pick systems, if the final END statement is left out, one is automatically assumed at the end of an item (remember, even programs are considered "items"). You should include the final END statement just for consistency. This END statement on line 47 terminates the program.
This finishes the first example. Before moving on to Example 2, take a closer look at the IF statement.
THE IF-THEN STATEMENT
The IF statement has perhaps the greatest variety of possible syntactical forms of any instruction in the language. Since it is one of the most frequently used statements, it is important to understand the mechanics of how and when they are used. The examples and discussions will first focus on the "simplest" forms. As you progress through the examples, more sophisticated cases and uses are uncovered. Example 1 illustrates the forms.
The Single-Line Form
The "single-line" form means that the entire logic test, and the instructions which are to be executed when it evaluates true, are contained in one physical source line. The first general form has a syntactical form which appears as:
IF conditional.expression THEN statement
If the conditional expression evaluates true (numeric nonzero), then the single statement after the THEN initiator is executed and program execution continues on the next line of the program. If it evaluates false (zero or null), program execution continues from the next line.
Since statements may be delimited by a semicolon, the next form of this occurs as:
IF conditional.expression THEN statement; statement; statement...
When this conditional expression evaluates true, all of the statements following the THEN initiator are executed. There is no logical limit to the number of statements that may be put here but, as a rule of thumb, use the multiline form of the IF statement when more than a few statements are to be executed.
The single-line form is useful when there is only one statement to execute upon evaluation. There are, however, many cases in programs where many statements need to be performed on a true condition. Rather than trying to place these all on one physical line with semicolons between the statements, there is the next logical extension to the IF-THEN construct.
The Multi-line IF-THEN Construct
The multiline IF-THEN construct has the general form:
IF conditional.expression THEN statement statement END IF conditional. expression THEN statement(s) . . . IF conditional.expression THEN statement(s). . . IF conditional. expression THEN statement(s) . . . END statement(s) . . . END statement(s) . . . END
Fig. 3-2. Nesting conditional statements.
The multiline form introduces the concept of the initiator/terminator relationship. When the THEN instruction appears as the last word in an IF- THEN statement, it is considered an initiator, meaning that it must be terminated later with an END statement. When the conditional expression evaluates false, program execution continues with the first executable statement after the END statement.
There are cases where "nested" IF statements are needed (Fig. 3-2). When they are the last word on a source line, remember to terminate every THEN or ELSE with an END.
Avoid having too many levels of nested IF statements. It makes programs more difficult to figure out and increases the probability of logic and syntax errors (see the use of the CASE construct in Chapter 9). Also, to assist in making the program more readable, try to align the END statement underneath the IF statement that initiated it.
Note that many instructions in the PICK/BASIC language allow (or require) the THEN and/or ELSE construct. Anywhere they are allowed/required, the single- or multiple-line forms may be used.
Note also the fact that on all versions of Pick the first END compiler directive encountered which is not part of a THEN/ELSE construct above, causes the program to stop compiling.
This concludes Example 1. Now it's time to take a review quiz on the principles discussed. (The answers are found in the appendix.)
REVIEW QUIZ 1
1) What is a variable?
2) How are PICK/BASIC programs entered into the computer?
3) After a program is entered, what two things must be done to activate it?
4) What are remark statements, and why are they used?
5) What characters may safely be used to separate multicharacter variable names?
6) What statement terminates the execution of a program?
7) What is a conditional expression?
8) How many programmers does it take to change a light bulb?