[home] Anguilla Library Computer Club

BASIC Programming Lesson #1

The point of this lesson is to introduce you to the BASIC programming language. A computer language is a way of specifying exactly what a computer should do, step by step, like a recipe for a very stupid cook. Find BASIC on your menu and run it. If you are already in BASIC, clear the previous person's program by doing ALT-F, N for New, then N to discard changes.

Activity: Magic Number Guessing Program

You will now write a game program. In programming, most keystrokes must be exactly right. If you make a typing mistake, the computer may complain when you finish the line, or later when you try to run the program, or not at all because what you typed by mistake is a valid program. This is the worst, since your program is not doing what you intended but the computer can't tell you. For example, in line 20 below, if you put = instead of + that is wrong, but not an error.
    let low = 0
    let high = 100
    print "--Write down a secret number between "; low; " and "; high

20  let guess = int ( ( low + high + .5 ) / 2)
    print "Is your secret number "; guess; "?"
    print "Answer L if ";guess" is too low, H if too high, C if Correct:";
    input answer$
    if (answer$ = "C") then goto 100
    if (answer$ = "H") then high = guess
    if (answer$ = "L") then low = guess
    goto 20

100 print "I got it!"
Think of a number between 0 and 100, but don't tell the computer. To play the number-guessing game, hit the following three keys one after the other: Alt R Enter. Alt brings up the menus, R selects Run and Enter selects Start. If you typed in the game correctly, it will be able to guess your secret number by asking you simple questions. When you type your answers to the games, press Enter after each line. This is what is should look like if your secret number is 75:
   --Write down a secret number between 0 and 100
   Is your secret number 50?
   Answer L if 50 is too low, H if too high, C if correct:L
   Is your secret number 75?
   Answer L if 75 is too low, H if too high, C if correct:C 
   I got it!
Did your program have any syntax errors when you tried to run it? This is usually due to typing mistakes. Ask your instructor for help. What happens if you enter lower-case "l" and "c"? Why?