Tiny BASIC

Interpreter and Compiler Project

Printing Things

One of the tasks almost any BASIC program will want to do is to put something on the screen. There's not much point in doing anything unless the user gets to see the result. The command for putting things on the screen is PRINT. So create a program called hello.bas with the following code:

10 PRINT "Hello, world."

Run it from the command line and you will see something like this:

$ tinybasic hello.bas
Hello, world.

Congratulations! You have written a Tiny BASIC program. You can print any text message by enclosing it in quotes this way. The quoted text is called a string. Tiny BASIC allows such strings only as part of a PRINT statement.

You can also do calculations as part of a PRINT statement. Create and run the following program (call it whatever you like):

10 PRINT 2+2

When you run it you'll see the result, 4. Calculations, or expressions, can use the four aritmetic operators +, -, * (multiply) and / (divide) and deal with integers, or whole numbers, only. If your calculation has a fractional result then it will be rounded towards zero, so 3/2=1 and -3/2=-1. This may frighten maths teachers and accountants, but we will have to learn to live with it.

You can combine srings and expressions in a single PRINT statement, so a more helpful version of the previous program would be:

10 PRINT "2+2=",2+2

You can have as many strings and expressions as you like in a single PRINT statement, in whatever order you need, like this:

10 PRINT "2 squared is ",2*2,", but 3 squared is ",3*3,"."

which would print:

2 squared is 4, but 3 squared is 9.

Notice that PRINT will put its output on a line on its own. If you want to print a series of things on a single line, they'll have to be in one PRINT statement like the example above. If we'd split the program up like this:

10 PRINT "2 squared is ",2*2,", "
20 PRINT "but 3 squared is ",3*3,"."

Then the output would look like this:

2 squared is 4,
but 3 squared is 9.

Most of us know our two and three times tables, so the examples so far haven't been particularly useful. Things become more interesting when we ask the user for values that we can then use in our calculations.

Next: Asking Questions

Comments

New Comment

Yes No