Basic Programming
In this unit we will look at a computer language, BASIC, and learn to write some simple programs using several BASIC commands.
How does a basic program get put together? How can you test the program?
First, you need to start the Basic programming environment, which is called
"True Basic". You'll find that if you open Macintosh HD, then the folder "TA
Classes", then the folder "Computer" and then "True Basic" folder. Double click
on the True Basic icon.![]()
Then, as you read along in the text, you can copy out programs from this text and try them out.
Here is a list of Basic Statements. Start with the first one, and work your way down the list...
END Statement
First, all Basic programs consist of STATEMENTS, and all basic programs must end with an END statement. If you forget, the computer will remind you before it lets you run the program. The very last statement of every program must be an END.
If you want the computer to remember something, you need to store it in a variable. (click on the blue for more details about variables).
You can put a value into a variable using the LET statement. For example:
LET count = 0
you can add to count using the statement:
LET count = count + 1
For more on what this means, click here.
You can do most any math function using LET, for example:
LET average = (test1 + test2)/2
or
LET celsius = (farenheit - 32) * 5/9
where * is the sign for multiplication and / is the symbol for division.
Once you have variables, you may want to print them. You can print in BASIC using the PRINT statement.
for example:
PRINT "Hello World"
This will print to the screen:
Hello World
or
LET average = 99
PRINT average
which will print to the screen the contents of the variable 'average', which in this case is 99.
or
PRINT "Here is the average:", average
which will print
Here is the average: 99
or even
PRINT "Here is your inflated average:", average+10
which will print
Here is your inflated average: 109
So, here is a what a simple program might look like:
LET celsius = 100
LET F = (celsius * 9/5) + 32
PRINT "When celsius temp is ",celsius
PRINT "The fahrenheit temp is ", F
END
The output of this program would be:
When celsius temp is 100
The fahrenheit temp is 212
Write a TRUE BASIC program to calculate the average of three numbers: 50, 65 and 85.
The program should add up the three numbers, divide by 3 and then print out the answer. The output should look something like:
The average of 50, 60 and 85 is 65
You must have at least one LET statement to calculate the average.
Before we write our next program, we need a way for the program to get information from the person using the program. The INPUT PROMPT statement will do the job. It looks like:
INPUT PROMPT "Question": <variable>
where <variable> will store the answer that the person using the program (the user) types in.
For example:
INPUT PROMPT "How old are you? ":age
or
INPUT PROMPT "What is your choice?":choice
You need to have the "quotes" around the question, and you need to have the colon ( : ) after the quotes and before the variable name.
Write a True Basic program to ask the user the question: "How old are you?" and let you type in the answer. Then have the computer print out your age back to you and then figure out how you will be in 10 years and print out the result. The program run should look something like this:
How old are you? 15 (<- the user, you, types in the 15, for example)
You are 15 years old.
You will be 25 in 10 years.
Obviously, if you type in 16, it should print back the 16 and 26 in 10 years.
Next, a computer can make decisions using the IF-THEN-ELSE statement, which looks like:
IF <condition> THEN
statement_1
statement_2...
ELSE
statement_3
statement_4...
END IF
The <condition> has to evaluate to a result that is either TRUE or FALSE. If the <condition> is TRUE, then statement_1 and statement_2 are done, and then the computer skips over statement_3 and statement_4. If, on the other hand, <condition> is FALSE, then the computer skips over statement_1 and statement_2 and instead executes statement_3 and statement_4.
For example:
IF (average > 85) THEN
PRINT "good job"
ELSE
PRINT "Study Harder"
END IF
The ELSE part is optional. The above IF statement will print "good job" if your average is above 85, and "Study Harder" if it is less than or equal to 85.
If statments can be more complex, for example:
IF (average >= 97) THEN
PRINT "A+"
ELSEIF (average >= 94) THEN
PRINT "A"
ELSEIF (average >= 90) THEN
PRINT "A-"
ELSE
PRINT "BELOW A"
END IF
Which will print your letter grade based on your numeric average. The computer tries each condition until one is met correctly, and then it does the statement after that condition and then skips past the END IF statement and proceeds.
The (condition) part of the if-then-else statement uses what are called BOOLEAN expressions. These expressions can be evaluated by the computer to be either true or false. Here are some of the allowed boolean operators:
a > b a < b
a >= b a <= b
a <> b this means: a does not equal b
Have the computer ask you how old you are. If you are over 18, it should print the statement "You can VOTE", and if you are younger than 18, then it should print "YOU CAN VOTE IN xxx YEARS", where xxx is the number of years until you can vote.
Here is a sample run:
How old are you? 16
You can vote in 2 Years.
Start up True Basic and type in your program. When you have finished typing it in, choose RUN from the Run menu or use the command-R shortcut. If you make mistakes, they will be listed at the bottom of your screen. Good luck.
Before moving to something more complicated, you should know how to put notes in your programs that are not part of the program itself. These are called "comments", and are used to make the program easier to read and understand. The computer doesn't care a bit about comments and will ignore them. You type in comments by placing an exclamation point anywhere on a line. Anything typed after an exclamation point (unless the ! is in quotes) will be ignored by the computer. For example:
! Here is a comment
LOOPS
Computers can repeat things over and over without getting bored. This kind of statement is called a loop. There are two ways to ask the computer to repeat steps. The first, a FOR loop which looks like:
FOR i = a TO b STEP c
NEXT i
A FOR loop is useful when you know in advance how many times you want to repeat something. The variable "i" is used for keeping track of how many times it has done the loop, and "a" and "b" are the low and high ends of this loop count.
For example:
For count = 1 to 10
PRINT count
NEXT count
will start with 'count' = 1, will print the value and then make count = 2 and then print it, and so forth until count exceeds 10, in which case it will finish the loop and proceed.
If you want to learn a little more about for-next loops, click here.
Computers are good at doing calculations. Sometimes you want to keep a running sum, say when you are adding up scores so that you can find the average. To do that, you need two variables, one to hold the current value, and another to keep a running sum. For example, to sum up a set of test scores, you might type in something like this:
INPUT PROMPT "How many tests? ": numtests
! the loop will cycle through 'numtests' times
FOR count = 1 to numtests
INPUT PROMPT "Score: ": testscore
LET total = total + testscore
! the above line keeps track of the total of all scores
NEXT count
! after the loop is done, you want to print the average
If numtests > 0 THEN
LET Average = total/numtests
PRINT "Your average is ", average
END IF
END
Using a FOR-NEXT loop, write a vote counting program. There are two candidates, SMITH and WESSON, and they are running for president. The program should
1) ask how many votes there will be
2) ask for the votes, one at a time. Tell the user to enter a 1 for each SMITH vote, and a 2 for each WESSON vote.
3) After all of the votes have been typed in, the computer should print out the vote tally for each candidate and announce the winner.
________________________________________
When writing longer, more complex programs, it is useful to break the program up into pieces, sometimes called subroutines. You have been writing in what is called the MAIN PROGRAM. The main program can CALL little sub programs that work together to perform various tasks. Typically, a subroutine should not be more than one page long, so it can be read easily by someone trying to understand how your program works.
To make a subroutine, you first must define it with the command SUB in BASIC. It must end with and END SUB
A subroutine could look like this:
SUB HELLOWORLD
PRINT "HELLO WORLD"
END SUB
Which will perform the obvious task of printing the words "HELLO WORLD".
In order to get the subroutine to run, you must CALL the routine. You can do this as follows:
CALL HELLOWORLD
Now on to the assignment. You will be writing an adventure game which will ask the user to respond to various questions and then proceed through the adventure. You should set up the path of your game using subroutines.
PRINT "Here is an adventure game about space"
PRINT "Where would you like to go?"
PRINT "Enter 1 for the Moon and 2 for Mars"
INPUT PROMPT "Choice ->":A
IF A = 1 THEN
CALL MOON
ELSE IF A = 2 THEN
CALL MARS
END IF
END
! This is the end of the main program
! Now the subroutines follow
SUB MOON
PRINT "YOU ARE ON THE MOON"
PRINT " 1 to leave the ship and 2 to stay"
INPUT PROMPT "Choice ->":A
If A = 1 THEN
CALL LEAVE
ELSE IF A = 2 THEN
CALL STAY
END IF
END SUB
Write an adventure game in the style shown above.
1) The game should have at least 5 branches where different questions are asked. You can have more, but no fewer.
2) There should be subroutines for each branch of the game, as shown in routine MOON in the previous example
HINTS!
1. Have only one of your branches be successful - all the others should fail!
2. Keep you lines short so you will be able to see all your sentences when your RUN the program.