Asking Questions
Most programs will interact with the user by asking them questions. Instead of calculating the squares of numbers we have chosen, we might ask the user to think of a number and calculate the square of that. When we ask the user for something, we need to store their answer somewhere so we can refer to it. This is where variables come in.
Variables are like boxes on a form. Tiny BASIC has 26 of them, and they have names: A, B, C and so on up to Z. Each of these variables can store a number. We can refer to them by their names in expressions, much as in algebra. To ask the user for a value for one of these variables, we use the INPUT command, as in the following example:
10 INPUT N 20 PRINT N
The INPUT statement lets the user type in a number and stores it in the variable N. The PRINT statement then prints out the number that was stored in N. So far, so useless. But let's combine this with what we learned about strings and expressions to make something a bit more interesting:
10 PRINT "Think of a number." 20 INPUT N 30 PRINT N," squared is ",N*N,"."
If you run this, and the user types, say 4 as their response, you will see something like this:
Think of a number. 4 4 squared is 16.
Next: Doing It All Again
Comments