Numbers are letters
By the end of this lesson you can print any character, and you know why the character 7 is not the number 7.
Every character has a number
Below are all 95 characters you can see on a keyboard, each key with its number under it. Press a few, then find the capital letter H and read its number:
Notice that the numbers follow the alphabet: 65 is A, 66 is B, and the small letters start again at 97. The digit 0 is 48, and a space is 32. A computer does not know letters, only numbers, so everybody agreed on one table that says which number is which character.
The usual name of this table is ASCII. The chart shows the characters with the numbers 32 to 126. Below 32 are characters that do something instead of showing something: number 10 starts a new line.
This program is 72 plus signs and one full stop. Press Play, or drag the slider all the way to the right, and watch the line Printed:
Notice that the letter H came out, and that the cell still holds 72. The full stop . is the print command: it prints the cell under the pointer as the character with that number. Printing does not change the cell.
What a program prints is called its output.
The next letter
The box holds the program of the last figure, which prints H. Add to it, so that it prints HI. The line under the figure says what it prints now:
Hint
I comes right after H in the table: number 73.
After printing, the 72 is still in the cell. One plus makes 73, and a second full stop prints I: .+.
Notice that letters which are neighbours in the alphabet are neighbours in the table, so going from one letter to the next costs a single +.
A digit is a character too
This program prints the character 0, with 48 plus signs. Change it so that it prints the character 7, and count how many plus signs it takes:
Hint
Find the digit 7 in the chart of step 1. Its number is not 7.
Hint
The digit 0 is number 48, and the digits follow each other.
The character 7 has number 55, which is seven more than the character 0, number 48. A cell that holds the number 7 prints something else: an old control character that used to ring a bell.
This matters every time a program works with numbers. To print a number from 0 to 9 as a digit, add 48 first. To calculate with a digit that was typed, take 48 off first. You will do both in Lesson 8 and Lesson 9.
Check yourself
A free exercise
Print the first letter of your own name. Look up its number in the chart of step 1. Nothing is checked here:
What you can do now
You can look up the number of a character and print it with ., and you can add 48 to turn a number from 0 to 9 into its digit. These are the commands you know, and the numbers worth remembering:
| Command | What it does |
|---|---|
+ − | Add 1 to the cell under the pointer, or take 1 off. |
> < | Move the pointer one cell to the right or to the left. |
. | Print the cell as one character. |
| Characters | Numbers |
|---|---|
| line break | 10 |
| space | 32 |
| the digits 0 to 9 | 48 to 57 |
| the capitals A to Z | 65 to 90 |
| the small letters a to z | 97 to 122 |
A small letter is always 32 more than its capital. The full table, with the control characters, is in the Reference.