Contents / Reference section / BASIC keywords / Previous letter / Next letter / Index
This sets up a test condition which can be used to control the subsequent action of the computer.
100 IF month=12 THEN PRINT "December"
100 IF A=1 THEN PRINT "One" ELSE PRINT "Not one"
100 IF answer$="BANANA" THEN PROCfruit
100 IF height<1.94 OR age<18 THEN GOTO 1030
100 IF length<>5 THEN 2140
100 IF RATE=5 THEN Y=6:Z=8 ELSE PRINT "Wrong rate"
100 IF month=11 THEN IF day=5 THEN PRINT "Guy Fawkes"
100 IF month=1 AND day=1 THEN PRINT "New Year"
100 IF X THEN Y=0
A statement forming part of the IF...THEN...ELSE structure. The word THEN is optional, as is the ELSE section.
IF <testable condition> [THEN] <statement>
or
IF <testable condition> THEN <line number>
THEN, ELSE
INKEY | input the number of the key pressed |
This function waits for a specified time whilst constantly testing to see if a key has been pressed on the keyboard.
If a key is pressed before the time runs out then the ASCII value of the key is given. IF no key is pressed in the given time then -1 is returned and the program continues. See the keyword ASC on page 207 for an explanation of ASCII values.
Note that a key can be pressed at any time before INKEY is used. All keys pressed are stored in a buffer in the computer and a character is removed from the buffer by, for example, the INPUT statement. You can "flush" the buffer of all characters by giving the command
*FX 15,1
The number in brackets, after the word INKEY, gives the amount of time that the computer must wait before giving up. The time is given in hundredths of a second, and may have any value between 0 and 32767.
In addition, the function INKEY can be used to see if a key is actually pressed at the instant the function is called. Normally pressing a key once enters the code for that key into the keyboard buffer. If the key is kept down then it will normally auto-repeat and further characters will be entered into the buffer. However, when the buffer is read with INPUT or GET or INKEY, you will have no idea how long the character has been waiting in the buffer. An alternative statement is provided which actually tests the keyboard rather than the buffer.
INKEY with a negative number in the brackets e.g.
INKEY(-27) will enable you to test to see whether a particular key is pressed at that instant. The number in brackets determines which key you wish to test. The following table shows the negative number to be used to test any particular key. Thus the letter L would be tested with PRINT INKEY(-87).
100 keynumber=INKEY(5)
220 result=INKEY(Y) X=INKEY(100)
A function which waits up to a specified time for a key to be pressed. The function returns -1 if no key is pressed in the specified time, or the ASCII value of the key pressed. The argument is the maximum time in centi-seconds.
<num-var>=INKEY(<numeric>)
GET, GET$, INKEY$
Key | Number | Key | Number |
f0 | -33 | 1 | -49 |
f1 | -114 | 2 | -50 |
f2 | -115 | 3 | -18 |
f3 | -116 | 4 | -19 |
f4 | -21 | 5 | -20 |
f5 | -117 | 6 | -53 |
f6 | -118 | 7 | -37 |
f7 | -23 | 8 | -22 |
f8 | -119 | 9 | -39 |
f9 | -120 | 0 | -40 |
A | -66 | - | -24 |
B | -101 | ^ | -25 |
C | -83 | \ | -121 |
D | -51 | @ | -72 |
E | -35 | [ | -57 |
F | -68 | - | -41 |
G | -84 | ; | -88 |
H | -85 | : | -73 |
I | -38 | ] | -89 |
J | -70 | ' | -103 |
K | -71 | . | -104 |
L | -87 | / | -105 |
M | -102 | ESCAPE | -113 |
N | -86 | TAB | -97 |
O | -55 | CAPSLOCK | -65 |
P | -56 | CTRL | -2 |
Q | -17 | SHIFTLOCK | -81 |
R | -52 | SHIFT | -1 |
S | -82 | SPACEBAR | -99 |
T | -36 | DELETE | -90 |
U | -54 | COPY | -106 |
V | -100 | RETURN | -74 |
W | - 34 | -58 | |
X | - 67 | -42 | |
Y | - 69 | -26 | |
Z | - 98 | -122 |
INKEY$ | input the character pressed |
This function waits for a specified time whilst constantly testing to see if a key has been pressed on the keyboard. If a key is pressed before the time runs out then the letter or number pressed is placed in the string variable. If no key is pressed in the given time then an empty string is returned and the program continues.
Note that a key can be pressed at any time before INKEY$ is used. All keys pressed are stored in a buffer in the computer and a character is removed from the buffer by, for example, the INPUT statement. You can flush the buffer of all characters by giving the command
*FX 15 1
The number in brackets, after the word INKEY$, gives the amount of time that the computer must wait before giving up. The time is given in hundredths of a second.
120 letter$=INKEY$(0)
384 result$=INKEY$(100)
920 X$=INKEY$(Y)
A function which waits for a key to be pressed within a specified period of time. The function returns a null string, if no key is pressed in the specified time. If a key is pressed the string returned consists of the single character pressed. The argument is the maximum time in centi-seconds.
<string-var>=INKEY$(<numeric>)
GET, GET$, INKEY
INPUT | to put information into the computer |
When a computer program is running there is often a need to get numbers or words from the outside world into the computer so that it can do calculations on these numbers or words. The statement INPUT is used for this purpose. There are a number of options:
100 INPUT X
will print a question mark on the screen and wait for the user to type in a number - for example "7". This is not very "friendly" - often it would be helpful to print a message on the screen before waiting for the user to type his/her reply. This can be done in two ways
340 PRINT "How old are you";
350 INPUT AGE
or more simply
340 INPUT "How old are you",AGE
If you do not wish the computer automatically to print a question mark then omit the comma between the message to be printed out and the variable to be filled in.
340 INPUT "How old are you" AGE
Often you may want to input several values one after the other. This can be done by placing the variables after each other, but separated by commas, thus
560 INPUT "Pick three numbers",X,Y,Z
When replying the user separates the values entered either with commas or by pressing the RETURN key after entering each value. The numbers that are typed in are placed in the appropriate variables - X, Y and Z in the example above.
The above examples all required numbers to be supplied by the user. INPUT can be used to get in words as well.
205 INPUT "What is your name",NAME$
You can INPUT more than one string at a time if you wish by using
200 INPUT "Town" ,A$,"Country",B$
INPUT LINE A$ will accept everything that is typed in including leading spaces and commas, and will place the lot into A$.
A statement to input values from the current input stream. The question mark prompt may be suppressed by omitting the comma following the prompt string. INPUT strips leading spaces off strings.
Too complicated for a useful yet simple description.
INPUT#
INPUT# | put information into the computer from cassette or disc |
It is possible to record data (numbers and words) on cassette or floppy disc where they can be stored for later use. The statement INPUT# is used to read the data back into the computer from the cassette or disc. See the section on file handling on page 188 for more information.
1200 INPUT# channel,date,name$,address$
3400 INPUT#X,U,V,W$
A statement which reads data in internal format from a file and places the data in the stated variables.
INPUT#<num-var>, <num-var>|<string-var>{,<num-var>|<string-var>}
OPENIN,OPENOUT,EXT#, PTR#, PRINT#, BGETO, BPUTO, CLOSE#
To search one string for any occurrence of another string, for example to see if one word contains another specific word.
The search normally starts from the beginning of one string but as an option the search can start from a specified point along the string.
The number returned is the string position of the second string in the first string. The leftmost character position is position number 1. If no match is found then zero is returned. A search for a null string X=INSTR("Sunday","") will always return 1.
240 X=INSTR(A$,B$)
put the position of B$ in A$ into X
180 Y=INSTR(A$,B$,Z)
start search at position Z.
PRINT INSTR("HELLO","L")
would print "3"
A function which returns the position of a sub-string within a string. The starting position for the search may be specified. There must be no space between INSTR and the first bracket.
<num-var>=INSTR(<string>,<string>[,<numeric>])
There is a known "bug" in release 1.0. If the second string is longer than the first string, for example,
X=INSTR("A","KNOWN BUG")
then the function will appear to work but will corrupt the stack. This bug is fatal if INSTR is used in procedures or functions.
LEFT$, MID$, RIGHT$, LEN$
This converts a number with a decimal part to a whole number. This function always returns a whole number smaller than the number supplied. Thus INT(23.789) gives 23 wheareas INT(-13.3) returns -14.
200 X=INT(Y)
1050 wholenumber=INT (decimalnumber)
330 pence=INT(cost * markup/quantity)
INT is a function converting a real number to the lower integer.
<num-var>=INT<numeric>