07.05.2015 Views

Download - True BASIC

Download - True BASIC

Download - True BASIC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

27<br />

Input Statements<br />

CHAPTER<br />

4<br />

Most useful programs have some flexibility built into them; that is, they can produce different results when provided<br />

with different data. Commonly, the user of the program provides this data and thus has some control over<br />

the program’s behavior.<br />

In <strong>True</strong> <strong>BASIC</strong>, the simplest way to obtain data from the user is the INPUT statement. The data provided by the<br />

user may consist of numeric or string values, and it may come from the keyboard or a file.<br />

This chapter introduces the fundamentals of user input. It discusses the INPUT statement, the LINE INPUT<br />

statement, and the GET KEY statement for getting single keystrokes. For information on graphical input from<br />

the GET MOUSE and GET POINT statements, see Chapter 13 “Graphics.” For input from menus, buttons, check<br />

boxes, special edit fields, and dialog boxes, see Chapter 14 “Interface Elements.”<br />

Basic Input<br />

Chapter 2 on “Constants, Variables, and Expressions” illustrates the use of the LET statement to assign data values<br />

to variables. Although it is extremely useful, the LET statement is limited in that you must know the value you wish<br />

to assign when you are writing the program. To change the value, you must change the program.<br />

Since it is impractical and undesirable for the user to change the source code before each run, <strong>True</strong> <strong>BASIC</strong> provides<br />

the INPUT statement to let the user assign values to variables during the run. The INPUT statement complements<br />

the PRINT statement. While the PRINT statement lets your program give information (the “answer”) to the user,<br />

the INPUT statement lets the user give information (the “question”) to your program.<br />

The INPUT statement pauses the program, prints a question mark (?) at the current text cursor position, and then<br />

displays the text cursor (if it is turned on). A visible text cursor indicates that the user must enter data before the<br />

program will continue.<br />

The number and type of variables specified as input items determine the number and type of values the user must<br />

supply. The program may include as many input items as necessary in an INPUT statement, separating them<br />

with commas. Consider the following example lines:<br />

INPUT a<br />

INPUT x, y<br />

INPUT name$, age, phone$<br />

The first line expects the user to enter a single number, the second line expects two numbers, and the third expects<br />

three pieces of information — a string, a number, and another string (in that order).<br />

The user must enter the proper number of items, of the proper types, in the proper order, and then press the Enter<br />

(or Return) key to tell the program to continue. Before continuing, the program matches the values entered with<br />

the list of input items from left to right. If the number and types of items do not match exactly, then <strong>True</strong> <strong>BASIC</strong><br />

prints a message and asks the user to re-enter the data. If they do match, <strong>True</strong> <strong>BASIC</strong> assigns each entered value<br />

to the corresponding input item. Thus, if a user responds to the third example line above by typing:<br />

? Chris Jones, 32, 555-4321<br />

then the program will assign the string value “Chris Jones” to name$, the numeric value 32 to age, and the string<br />

value “555-4321” to phone$. Notice that the user must separate multiple items by commas.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!