How Are You? And Other Questions Your Computer Will Ask.

So far, we have learned to use the PRINT command to get the computer to print lines of text. This lesson will show you how to get the computer to ask you questions. This is done with the INPUT statment. Like the PRINT command, INPUT allows text to be displayed. However, it also allows the user to enter data and temporarily store it in the computer.

10 INPUT "This is what would be printed on the screen";variable

The INPUT command can be used to enter numbers or words (strings). Numbers can be added, subtracted, multiplied, divided, and be used in othe math functions. If a variable is wished to be used this way, the variable name may not be a string. Using the PRINT command, we can return the value of the varible as output. We do this by using the PRINT command followed by the variable name. There are not quotations around the variable name, however, PRINT may use both quotation text and variables in the same line.

10 INPUT "This is a number variable"; Variable
20 PRINT "This text is printed, and then the variable is printed" Variable
30 PRINT "There is no space between the last line of text and the variable unless there is a space inside the quotes"

Number variables may be used as any other numbers simply by placing the variable name where the number would normally be.

10 REM assume varible ONE and Variable TWO are five
20 PRINT 5 + 5
30 PRINT ONE + TWO
40 PRINT "Both these lines return 10"

The other variable that may be used in conjunction with INPUT, are string variables. String variables are used in much the same way as numbers, however, strings can contain letters and spaces as well as numbers. Strings cannot do mathmatic operations (although strings can become numbers as discussed in later lessons). All string variable names must end in the $ sign.

10 PRINT string$
20 PRINT "This would print the characters stored in string$. Note that it ends with a $"

The rules of text with the INPUT statment are much the same as with PRINT, although use of commas to tabe over or lines of text separated by variables do not work. Also, in Basic, each input statment will return a ? at the end of any text line, or even if no text is used.

10 INPUT "When displayed, there would be a question mark at the end of the line." ;variable

PRINT can also be used to print variables. This will be discussed in the chapter on INPUT statements. However, It should be known that PRINT can be used on the same line to print text, variables, and more text as follows.

10 PRINT "Text can go here " variable " with a variable in the middle, or vice versa, or in any combination"

Thats all for this lesson, however, be sure to check back for more. In the mean time, write a program using INPUT. Example 1: write a program that asks the age and name of a person, and then displays it. Example 2: write a program that asks for a persons name and phone number.

QBasic Home



ianmacallen.com | Annex | Archive

All content Copyright 2006 Ian MacAllen Unless otherwise Atrributed.
Contact: ianmac47@hotmail.com