3.2: Use Your Words

At this point, as well as being able to see this page, you should also have the BASIC terminal open in another tab. If you don't, simply press the yellow "BASIC" button at the top, or press the button below:

Open BASIC Terminal

Brilliant! Let's do this.

The way BASIC works is that it won't do anything unless you tell it to. And you tell it what to do by writing little notes to it and pressing ENTER. After you do this, it replies to you.

Remember in the last section when we tried to tell it "HELLO" and it just said "No such variable":

Soc Computer 32k
 
BASIC
 
>HELLO!
 
No such variable
>
Well, "no such variable" to you to, I guess

That's because it doesn't understand what "hello" means. Like Jacquard's Loom and the Harvard Mark I, it needs to be spoken to on it's own terms.

Here's something it will understand. Try typing in this and pressing ENTER:

>PRINT "HELLO"

Now that should have made it reply with this:

>PRINT "HELLO"
HELLO
>

That's more like it! This works because "PRINT" is one of the command words that BASIC understands. The command was designed by the creators of BASIC to make it repeat back whatever you put in the double quotes after, in this case, "HELLO".

Try it without the quotes and see what happens:

>PRINT HELLO

You would have got the same error again:

>PRINT HELLO
 
No such variable
>
Now you're just repeating yourself, BASIC!

That's because although it understood the "PRINT" command, it still didn't understand the word "HELLO" after. You can use any phrase as long as it's in double quotes. Try this for example:

>PRINT HELLO
 
No such variable
>PRINT "I AM A COMPUTER"

And that works:

>PRINT HELLO
 
No such variable
>PRINT "I AM A COMPUTER"
I AM A COMPUTER
>

Hehe, you just made it say "I AM A COMPUTER".

So we are kind of learning how to speak BASIC's language. But this isn't really programming yet, it's more like a conversation with a parrot. It's just repeating everything we say as soon as we ask it to say it.

What we really need it to do is store what we want it to do so it can be run later, like the pieces of punched card in the Harvard Mark I.

This is possible in BASIC, and luckily for us you can do it with no pieces of card required.

So lets make this more of a computer programme. You write programmes in BASIC one line at a time, always starting off by putting in the number of the line you're writing.

We're going to start off by writing the first line of our programme, so we start off by putting in the number 1. DO NOT press ENTER yet, though:

>1

If you did press ENTER by accident, don't sweat it. Just type 1 on the new line it gives you.

By putting this "1" at the beginning of this line, you are telling BASIC that what comes after this will be the first line of your programme.

This is how BASIC was designed. Lines that start with a number will end up being saved as part of your programme, lines that don't will be actioned immediately.

To show this, let's try putting a PRINT command after after the 1. But this time, instead of saying hello, we'll put in the setup for a joke:

>1 PRINT "I'M AGAINST PICKETING"

Now you can press ENTER.

Notice how this time, nothing happens. That's because the line has been saved as part of our programme. It is line 1 of our programme, to be precise.

We can check this by asking BASIC to tell us what is currently saved in our programme. To ask it this, simple type in the word LIST:

>1 PRINT "I'M AGAINST PICKETING"
>LIST

And press ENTER. You should get the following:

>1 PRINT "I'M AGAINST PICKETING"
>LIST
   1 PRINT "I'M AGAINST PICKETING"
>

Can you see what it's done? It's told us what our current programme looks like at the moment. It's saying "line 1 says print I'm against picketing". You'll notice that the reply was indented slightly. This is to make it clear that this is the result of you caling LIST, rather than it being something you typed earlier, like in the previous lines which are still visible on the screen.

This will become more clear when we add a second line to our programme. Try adding this:

>1 PRINT "I'M AGAINST PICKETING"
>LIST
   1 PRINT "I'M AGAINST PICKETING"
>2 PRINT "BUT I DON'T KNOW HOW TO SHOW IT"

And press ENTER. Again, it doesn't look like much has happend:

>1 PRINT "I'M AGAINST PICKETING"
>LIST
   1 PRINT "I'M AGAINST PICKETING"
>2 PRINT "BUT I DON'T KNOW HOW TO SHOW IT"
>

But try running LIST again now (and pressing ENTER):

>LIST
I know we don't always show the previous lines in our illustrations - this is just to save space on the page. What's important is that you are always typing in the bit we show in yellow.

Noice the reply now looks something like this:

>LIST
   1 PRINT "I'M AGAINST PICKETING"
   2 PRINT "BUT I DON'T KNOW HOW TO SHOW IT"
>

Does this make it more clear what LIST is doing? LIST shows us all the lines of our current programme, so we can check our work. It's the modern day equivalent of taking the punched cards and examining them before feeding them into the machine.

So how exactly do we "feed" our programme into "the machine"? We've come a long way since the Harvard Mark I. There's no physical cards, and the machine now consists of a screen and a keyboard.

With BASIC, we can now run our code with another command: RUN.

Try typing in RUN and pressing ENTER:

>LIST
   1 PRINT "I'M AGAINST PICKETING"
   2 PRINT "BUT I DON'T KNOW HOW TO SHOW IT"
>RUN

You get this:

>LIST
   1 PRINT "I'M AGAINST PICKETING"
   2 PRINT "BUT I DON'T KNOW HOW TO SHOW IT"
>RUN
I'M AGAINST PICKETING
BUT I DON'T KNOW HOW TO SHOW IT
>

Can you see what's happened? Typing in RUN and pressing enter literally "ran" our programme. BASIC went through the stored lines of our programme, all two of them, and ran them in the order they were numbered.

You can RUN the programme as many times as you like. Go ahead, try using the RUN command a couple more times. Because your programme is saved in the memory, the lines will be run every time.

Can you see what else has happened? You just wrote your very first computer programme, that's what! You are now officially a coder! Well done!

Let's put the cherry on top by giving it a cool name, perhaps "Joke Robot", and adding a sign off at the end. Try this:

>0 PRINT "HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE..."
>3 PRINT "GOOD NIGHT EVERYBODY!"
>

Make sure you press enter after typing in the lines.

Can you see what we are doing here? We are adding a line at 0 (yes, we can do that!) that will print a little intro, and adding a line at 3 that will print a little outro.

Lets take a look at what our whole programme looks like now with LIST:

>0 PRINT "HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE..."
>3 PRINT "GOOD NIGHT EVERYBODY!"
>LIST

And we get this:

>LIST
   0 PRINT "HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE..."
   1 PRINT "I'M AGAINST PICKETING"
   2 PRINT "BUT I DON'T KNOW HOW TO SHOW IT"
   3 PRINT "GOOD NIGHT EVERYBODY!"
>

Notice how the lines appear in the correct order, even though we started by typing lines 1 and 2, and then did lines 0 and 3. Because we were clever with our numbering, they are saved in the correct order.

Go ahead and run that. If you haven't got any errors, you will get this:

>RUN
HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE...
I'M AGAINST PICKETING
BUT I DON'T KNOW HOW TO SHOW IT
GOOD NIGHT EVERYBODY!
>

But What If I Got An Error?

If you made any mistakes, you might get an error. Maybe something like this:

>RUN
HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE...
 
No such variable on line 1
>

If you get an error, no problem! If you RUN a programme and there is any error, BASIC will tell you exactly which line the problem was on. So simply run the LIST command, check the offending line, and work out where you went wrong.

In my case while I was experimenting, I had that error, so I ran LIST:

>RUN
HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE...
 
No such variable on line 1
>LIST
   0 PRINT "HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE..."
   1 PRINT "I'M AGAINST PICKETING
   2 PRINT "BUT I DON'T KNOW HOW TO SHOW IT"
   3 PRINT "GOOD NIGHT EVERYBODY!"
>

The error told me I had a problem on line 1, so I looked at line 1 and... yep, can you see it my mistake? No double quote after the word picketing! So all I had to do was have another go at writing line 1:

>RUN
HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE...
 
No such variable on line 1
>LIST
   0 PRINT "HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE..."
   1 PRINT "I'M AGAINST PICKETING
   2 PRINT "BUT I DON'T KNOW HOW TO SHOW IT"
   3 PRINT "GOOD NIGHT EVERYBODY!"
>1 PRINT "I'M AGAINST PICKETING"
>

That's how BASIC works, you can replace lines by writing them again.

Then I used LIST again to check it looked ok:

>RUN
HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE...
 
No such variable on line 1
>LIST
   0 PRINT "HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE..."
   1 PRINT "I'M AGAINST PICKETING
   2 PRINT "BUT I DON'T KNOW HOW TO SHOW IT"
   3 PRINT "GOOD NIGHT EVERYBODY!"
>1 PRINT "I'M AGAINST PICKETING"
>LIST
   0 PRINT "HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE..."
   1 PRINT "I'M AGAINST PICKETING"
   2 PRINT "BUT I DON'T KNOW HOW TO SHOW IT"
   3 PRINT "GOOD NIGHT EVERYBODY!"
>

I couldn't see any more mistakes so I ran it again, and it worked!

>RUN
HELLO! I AM THE JOKE ROBOT! HERE IS MY JOKE...
I'M AGAINST PICKETING
BUT I DON'T KNOW HOW TO SHOW IT
GOOD NIGHT EVERYBODY!
>

You'll make mistakes all the time while coding and the computer won't explode in a ball of flames. Making mistakes is considered part of the process, which is why BASIC gives you a useful error message when is happens.

Nice Job!

Whether you got any errors or it worked first time, one thing is for sure: your amazing first programme, the "Joke Robot", is finished. You can run it as many times as you like with the RUN command. Go ahead.

You can keep running the programme until the joke stops being funny!

That's as long as people get it in the first place of course, sadly not everyone did...

Some people have no sense of humour.

Looking Forward...

So that's how to get your computer to show text with BASIC. Next up we'll be looking at... wait... I'm getting another incoming message... oh dear me what now...

Wow my right foot is going to be so happy!

So I guess that's the word we will use from now on then. That was tough, and I'm not sure the prize was worth all the stress... I think I'll volunteer you next time.

Ok let's really move forward now...

Looking Forward... (for real this time)

Getting a computer to show text is one of the most important things you can do. Websites and messaging services would be very difficult to use if they couldn't display text. But of course, computers can do more than that. Click the button below to move in to our next lesson, and see what else we can do with BASIC:

Continue to 3.3: Just A Number