3.3: Just A Number

We've seen how to use strings in BASIC to produce text on-screen. In this section, we will look at working with numbers.

In the last section, we used strings to PRINT words in BASIC, like this:

>PRINT "HELLO"
HELLO
>

But sometimes, we specifically want to work with numbers, rather than words.

Why do we need to make the distinction? Have a go at this (and press ENTER). Notice that there are NOT any double quotes being used:

>PRINT 1

You will get this:

>PRINT 1
        1
>

You'll notice that this is similar to what happened with the string, but a little bit different. We didn't need to put in any double quotes around the 1, and when BASIC did print the number, it added EIGHT (yes eight, I counted them!) spaces before it.

Usually, when we try to PRINT something without the double quotes, we get that "No such variable" error. However, this time, it not only printed the number but put eight spaces before it.

That's because BASIC recognised 1 as a number, and not string.

So whats the big deal here? Why all this special treatment because it's a number? We can still do the same thing as before and put quotes around the 1:

>PRINT 1
        1
>PRINT "1"

And this prints the 1 without the spaces:

>PRINT 1
        1
>PRINT "1"
1
>

Which seems to be prettu much the same result, just without the spaces, right? So what's the difference between the two? Who cares? What's so special about a number?

The difference is that when BASIC understands that its looking at a number, you can do MATH.

Give this a try:

>PRINT 1 + 1

Look how this gives you the correct mathematical answer:

>PRINT 1 + 1
        2
>
Thank goodness we have a computer to help us figure this out

But look what happens if we try this same thing, but put in the 1s as strings:

>PRINT "1" + "1"

That gives us this:

>PRINT "1" + "1"
11
>

Can you see what's happened here? BASIC allows us to add two numbers, OR two strings, but adding two numbers does it mathematically while adding two strings simply joins them together.

With most strings this would make sense, for example:

>PRINT "BUTTER" + "FLY"
BUTTERFLY
>

As that gives us "BUTTERFLY".

Learn, But Do Not Over-learn

At this point, you might be wondering why on earth you would want to join two strings together like this. After all, it wouldn't exactly be hard to write the command "PRINT 'BUTTERLY'". Don't worry about it for now, it's not worth going into while we discuss strings vs numbers. You'll learn why this is useful later.

Also, hi! I am the "Learn But Do Not Over-learn" box! I will show up now and then because sometimes, when learning coding, we're forced to introduce two new concepts at once in order to properly learn just one of them.

This is necessary because sadly there isn't always a sensible order to learn things in, so sometimes we need to learn a tiny bit of one new thing in order to fully learn another new thing.

When this happens, I will appear to assure you that we only need to focus on one thing for now, and clarify what that one thing is. We can worry about the other thing later.

But it can get confusing when our strings only contain numbers, giving us "1" + "1" = "11". This happens because BASIC does not care one bit about what you put in your strings. If they are in double quotes, they will be regarded as strings, and so treated like strings, and so will be joined together like strings.

This is why we need to tell BASIC when something is a number. We do this by simply NOT including double quotes.

And this is also why, when you do PRINT a number, it has a load of space before it. This is to remind you that this is a number you are looking at, rather than a string.

When working with numbers, we can do other math operations too. Try these: (hit ENTER after you type the bit in yellow)

>PRINT 1 - 1
        0
>

That's subtraction. We can also do multiplication, we just have to use an asterisk as the multiply symbol:

>PRINT 3 * 2
        6
>

That's because there isn't a key on a standard keyboard for the multiply symbol: ×. Yes, it does look a bit like the letter "x", but they are different symbols and using "x"" as a letter and a math symbol would make things too confusing in coding. So we use an asterisk which is shift + 8 on most keyboards, but might be different for you depending on what country you're in.

We can also do division. Similar to multiplication, there isn't a divide symbol: ÷ on a standard keyboard, so we use a forward slash: / instead:

>PRINT 6 / 2
        3
>

And last but not least, we can use brackets to group up smaller calculations to make bigger ones:

>PRINT (4/2) + (4/2)
        4
>

We now have enough to work on our next programme...

Could I Win The Lottery?

Have you ever thought about what you might do if you won the lottery? I'm pretty sure you have.

I know exactly what I would do if it was me. I wouldn't buy anything new, no mansion or expensive car or anything like that. I've seen the horror stories of what some people do with their money before getting robbed by their own family. Instead I'd hire a team of people to do all the things I keep procrastinating, things like:

  • house cleaning
  • clothes laundry
  • bed making
  • paying bills
  • booking dentist appointments
  • renewing the car / house / pet / boiler / building / contents insurance
  • washing the car
  • remembering birthdays / anniversery / valentines / mothers day / fathers day / buying the presents
  • anything to do with the Inland Revenue
  • etc

...allowing me to spend the rest of my days eating crisps and listening to podcasts.

But what would you spend £12m on? Probably none of that, you'd use the money on something way cooler. So it would be great to win it, but what are your chances of actually hitting the jackpot?

In the UK National Lottery they draw six balls numbered 1 to 59. Knowing this, we can use BASIC to write a programme that calculates our chances of winning it.

But first we need to start a new programme. If you run LIST now, you might find you still have the code from the last session loaded in the memory:

>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!"
>
We don't have time for you now Joke Robot, there's money at stake!

We need to get rid of this so we can make our new lottery programme.

To start making a new programme in BASIC, simply type NEW and press ENTER:

>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!"
>NEW
>

That's it, that's all you need to do to start a new programme. You can check this by trying LIST again:

>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!"
>NEW
>LIST
>

See how the reply from BASIC is now empty? That's because the old programme has been erased. We can still see traces of it in the "chat history" above, but we won't be able to get BASIC to load those lines again. They are gone forever.

Which happily allows us to begin our lottery programme. Try this:

>1 PRINT "ARE YOU FELLING LUCKY? HERE IS YOUR CHANCE OF WINNING THE LOTTERY:"
>2 PRINT "YOUR CHANCE OF WINNING IS EXACTLY 1 IN:"
>3 PRINT (59*58*57*56*55*54) / (6*5*4*3*2*1)
>

Line 3 is the interesting bit as it is the calculation required for working out your chance of becoming a millionaire.

Learn, But Do Not Over-learn

Don't worry too much about the calculation itself, it works because of math. 59 × 58 × 57 × 56 × 55 × 54 is the chance of guessing six balls 1 - 59 IN THE CORRECT ORDER THEY ARE DRAWN, which of course isn't a requirement to winning the lottery, we just need to get the numbers right in any order. So we "cancel out" this ordering problem by dividing that total by the total possible number of orders our guess of six numbers could cover, which is 6 × 5 × 4 × 3 × 2 × 1.

Again, don't worry if that doesn't make sense. Just understand that this is the math involved in calculating the likelihood of winning the lottery.

RUN this programme and you get:

>RUN
ARE YOU FELLING LUCKY? HERE IS YOUR CHANCE OF WINNING THE LOTTERY:
YOUR CHANCE OF WINNING IS EXACTLY 1 IN:
        45057474

Hmmmm. So our chances of winning are slim it seems. Good thing we have BASIC.

But the prize is worth it right? After all, I'd never have to make my bed again! The idea of the prize is simply too alluring. I think we're going to have to give it a go.

But I'm rubbish at picking lucky numbers. Is there any way we can get some help picking them?

Move on to the next section to see how using BASIC might just help us get one step closer to winning our millions anyway...

Continue to 3.4: Random Numbers