Back To Basics - Part 3

Entering and printing, input and output. Having got the basics out of the way, Mark Moxon gets down to the serious stuff and invites you to converse with your computer

Last month was a killer: no doubt about it. Variables are fundamental, but unfortunately not terribly interesting. This is where the fun starts, so tighten your seat-belt, and prepare to learn how to talk to your computer.

Input and output

Before talking about more variable operators (as promised last month) let's have a look at entering and printing, input and output. This is the crunch: there's not much use for a computer which can't communicate with its lord and master.

You've already seen the PRINT command in action for printing out variable names. Well, there's a lot more to it than that, so let's do a little experimenting. The PRINT command is followed by a list of items to print: these items can be strings, variables or expressions, and they are separated by so-called print formatters: the latter simply determine how the items are shown on screen.

Right, now for some action. Type in and run Listing 1.

Listing 1

REM >Listing1
:
ON ERROR REPORT:PRINT " at line ";ERL/10:END
MODE 0
PRINT "Semi-colon ";"Semi-colon ";"Semi-colon "
PRINT "Comma ","Comma","Comma"
PRINT "Apostrophe "'"Apostrophe "'"Apostrophe "
PRINT TAB(17);"Tab to 17 spaces"
PRINT TAB(17,15);"17 across, 15 down"
:
var=123.45
PRINT '"Variable value is ";var
PRINT "Commas",var,var

The first thing you'll notice is that the screen mode changes to Mode 0. If your monitor can't display Mode 0, change the number in line 4. This mode change is done by the MODE 0 command, and I've put it in so we can see the results of printing more clearly.

Printing in detail

Line 5 and onwards contain various PRINT commands, with each line showing off a particular print formatter. Here's a line-by-line description of what's going on: lines 5 to 9 deal with printing strings, and lines 12 onwards with numbers.

Line 5 shows the semi-colon in action. If two items are separated by a semi-colon, they are printed one after the other on the same line, so PRINT "1";"2" does exactly the same as PRINT "12".

Line 6 contains a comma. If you've used a wordprocessor, you'll be familiar with the concept of tabs: every ten spaces (or whatever).

A comma in a PRINT statement means 'move to the next tab position to print the next item'. The default tab position is every ten characters, so the example prints the word 'comma' every ten characters along the screen.

Line 7 uses an apostrophe. This is an easy one: it means 'move onto the next line'. This is similar to pressing RETURN in a wordprocessor.

Line 8 introduces the TAB operator in its simplest form. The TAB(17); part means 'move along to the 17th character position along this line, and print'. Simple.

Line 9 shows the TAB operator in its meanest form, with two numbers. Putting TAB(17,15); in the PRINT statement means 'move to the 15th line on the screen, then move along 17 characters, and print'. Note that this form of TAB doesn't care which line you last printed on: it goes to a specific position on the screen and prints.

Line 12 shows that you can include print formatters anywhere in the line; the apostrophe at the start of the line means 'skip a line', and if you look when you run the program, there is a blank line.

Line 13 uses the good old comma, but this time with numbers, and there's a definite difference: notice how the 123.45s and the Commas don't line up vertically. This is because with numbers, the computer moves along to the next tab and then prints the number so that it ends at the next tab. If you look, you'll see that the 123.45s end just before the Commas start. Now to input.

Give me input

The only way we've been able to give variables values is to declare them in a statement like var=123.45.

That's fine if you know the values of all your variables in advance, but what if you need to ask the user for information? This is where the INPUT command comes in. Again, it's best to learn how by example, so type in Listing 2.

Listing 2

REM >Listing2
:
ON ERROR REPORT:PRINT " at line ";ERL/10:END
MODE 0
:
INPUT var
PRINT "You typed: ";var
:
INPUT "Give me a number " var
PRINT "You typed: ";var
:
INPUT "Give me a string " var$
PRINT "You typed: ";var$
:
INPUT "Question mark",var$
INPUT "No Question Mark " var$
INPUT TAB(17,15);"Middle of screen " var$
INPUT "Lots of variables " var1,var2,var3
PRINT "Values: ";var1;" ";var2;" ";var3

The INPUT command is, in its simplest form, simply followed by the name of a variable (this can be any name: it doesn't have to be the name of an existing variable). What this command will do is print a question mark, and will wait for you to type something in, followed by RETURN. The value you type in will then be assigned to that variable. So, when you run Listing 2, type in a number at the prompt (with a decimal point if you want) and the variable var has a value.

In this case the variable in line 6 is numeric, so if you type in something that isn't a number, you'll end with the value 0. Here's a look at the other forms of INPUT.

Line 9 shows how to include prompts: just put the prompt string before the variable name. This prompt string can include all print formatters for PRINT above.

Line 12 shows how to input strings, in this case into the variable var$.

Line 15 has a comma after the prompt string, which appends a question mark to the prompt. Line 16 doesn't have the comma, and doesn't have a question mark added.

Line 17 shows TAB in INPUT statements. It behaves like TAB in PRINT statements: this line prints the prompt on the 15th line, 17 characters in.

Line 18 has a list of variables, rather than just one. When this line is executed, the prompt appears. If you enter one value, a question mark will appear, waiting for the second value, and then the third.

You can enter all three values in one, by separating them by commas.

Enter 1,2,3 then RETURN, and van, var2 and var3 will be assigned values of 1, 2 and 3, in one go.

String Operators

Operator Description
ASC("c") Evaluates to the ASCII code of the character c. Every letter has a unique number, and this works it out.
CHR$(n) Takes the number n and returns the character which has ASCII code n. If it helps, the following is true: CHR$(ASC("c"))="c"
LEFT$(s$,n) Returns the leftmode n characters of the string s$, so LEFT$("12345678",4) evaluates to "1234".
MID$(s$,m,n) Returns the segment of n characters from the middle of a string, starting from the mth character. Therefore, MID$("12345678",2,5) evaluates to "23456".
RIGHT$(s$,n) Returns the rightmost n characters of the string s$, so RIGHT$("12345678",4) evaluates to "5678".
STRING$(n,s$) Evaluates to a string made up of n consecutive copies of s$, so STRING$(10,"Aa") gives "AaAaAaAaAaAaAaAaAaAa".


Source: Acorn User 135 - October 1993
Publication: Acorn User
Contributor: Mark Moxon