Probe

95









Based on an Apple II Text by

Scott Foresman



Written by

D.J. Bouwsma and B. Griece

Westminster Christian School

1995

1 Getting Started



Activity 1

When you turn on the computer, you will see a menu with several categories listed on it. The menu you want to use is Computer, so Press C to get to it, and then press the key. Once in the Computer menu, use the arrow keys to highlight HS Programming, and press . Now, press M and to highlight MS-DOS QBasic, and press . Press ESCAPE to clear the Dialog box.



Type these lines on the computer, using your own name. Press the key after you type each line.



Go to the immediate mode by typing Shift-F6 and type this program.



PRINT "your name"

PRINT "MY NAME IS your name."



Usually, you will want to run most programs in the normal mode. To run programs in the normal mode press Shift-F5.



The computer will print everything you type inside the quotation marks (called a string). If you make a typing mistake, use the back arrow key (<--), which is located above the to correct it. Now type these lines using your name and see what happens. Remember to press after each line.



PRINT "n a m e"

PRINT " name"

PRINT "name"

PRINT "name "

PRINT "na me"

PRINT "namenamename"

Predict the output (what the computer prints on the screen) for each line below.



1. PRINT "COM PUTERS"



2. PRINT " ARE"



3. PRINT VERY "



4. PRINT "VERY FAST"



5. PRINT "M A C H I N E S."



Programming Tip: If you forget the first quotation mark (as in line 3 above) or both quotation marks, the computer will print a zero. You don't need to type the end quotation mark if it is the last character in the line before you press . However, it is good programming practice to use both beginning and end quotation marks in all your PRINT statements.



Write these lines as PRINT statements.



1. Today's date



2. Your name



3. Your street address



4. Your city



5. Your state and Zip Code



6. Your birth date



7. Your grade



8. Your school



9. Your favorite food



10. Your favorite sport







The computer remembers lines you number. In QBasic, however, it is not necessary to use line numbers. You may use any characters you wish as long as it is followed by a colon, or none at all, but line numbers are used in this book for clarity. ALT, F N clears any previous programs from the computer's memory. END shows the computer where to stop.



ALT, F N

10 PRINT "I CAN"

20 PRINT "WRITE"

30 PRINT "A PROGRAM."

40 END



Now type SHIFT F5 to see your program. What happens when you type SHIFT F5 again?





Now write a program that prints the information about yourself that you filled in on lines 1-10 above. CLS moves the cursor to the top of the screen.



ALT, F N

CLS

10 PRINT "TODAY'S DATE IS ."



20 PRINT "MY NAME IS ."



30 PRINT "I LIVE AT ,"



40 PRINT " ,"



50 PRINT " ."



60 PRINT "MY BIRTH DATE IS ."



70 PRINT "MY GRADE IS ."



80 PRINT "MY SCHOOL IS ."



90 PRINT "MY FAVORITE FOOD IS ."



100 PRINT "MY FAVORITE SPORT IS ."



110 END



Type SHIFT F5 to see your program. Some of your lines may have "wrapped around" the screen. There is room for 80 characters (letters, numbers, and symbols) and spaces across the screen.



Each program remains in the computer's memory until you type ALT, F N or turn off the computer.



Now add this line to the program between line 100 and 110. Do not type ALT, F N.



105 GOTO 10



Run the program. What do you see on the screen?







To stop the program, hold down the CTRL key and press BREAK (located along the top right of the keyboard. Sometimes you may be required to press SPACE or also). The program now has a loop in it. Line 105 tells the computer to go back to line 10 and repeat the program. It does this again and again until you tell it to stop. This kind of loop is called an infinite loop.





Programming Tip: When you press CTRL-BREAK to stop the running of a program, the computer tells you at which line the program was interrupted. Press F5 to continue running the program from the point where it was stopped.



What happens if you make the program go back to a line other than line 10?







Add these between the lines of your program.



15 PRINT

25 PRINT

35 PRINT

45 PRINT

55 PRINT

65 PRINT

75 PRINT

85 PRINT

95 PRINT

103 PRINT



Now run the program with the new lines. Use CTRL-BREAK to stop. What does the PRINT statement do when it is typed with nothing after it?





Programming Tip: Line numbers are optional. They only need to be used when a GOTO command (or some similar command) is used.



Activity 2



Programming Tip: To save time and typing, you can type a question mark (?) instead of the command PRINT. Experiment by typing ? "your name" on the computer. You will see that the computer lists the word PRINT wherever a question mark was typed (unless the question mark is contained inside the quotation marks of a string).



Type these lines in the immediate mode. Use a question mark to stand for PRINT.

PRINT "A","B"

PRINT "A","B","C"

PRINT "A","B","C","D"

PRINT "A","B",

PRINT "A","B","C"



Now use your first name to run this program.



ALT, F N

5 CLS

10 PRINT "your name",

20 GOTO 10

30 END



Use CTRL-BREAK to stop the program. Change the comma at the end of the line 10 to a semicolon. To change a line in a program, simply retype the line.



10 PRINT "your name";



Run the program. Write what each symbol tells the computer to do.



comma



semicolon



Programming Tip: It is not always necessary to use an END statement at the end of all programs. However, it is a requirement in some cases, so using it is a good habit to develop. In this book you will find an END statement at the end of all programs, even those with infinite loops, in which the program is stopped manually.



Predict the output of these programs, then run them on the computer. Type the lines exactly as they are shown.



10 CLS

20 PRINT "COMPUTERS ";

30 PRINT "ARE ";

40 PRINT "GREAT."

50 END



ALT F,N

5 CLS

10 PRINT "BE CAREFUL . . ."

15 PRINT

20 PRINT "THIS IS","TRICKY."

30 END



Write a program that will print the following headings and information. Remember that each of the three print fields should have 7 or fewer characters and spaces in it.



FIRST LAST YEAR OF

NAME NAME BIRTH



ABRAHAM LINCOLN 1809

SUSAN B. ANTHONY 1820

HARRIET TUBMAN 1820



10 HOME



20 PRINT___________________________________________________



30 PRINT ______________________________________________________

40 PRINT ______________________________________________________



50 PRINT ______________________________________________________



60 PRINT ______________________________________________________



70 PRINT ______________________________________________________



80 END



Run the program, then add some names and dates to the list.



Type this program on the computer.



10 CLS

20 PRINT "HI, MY NAME IS"

30 PRINT

40 PRINT "YOUR NAME"

50 END



Now add these lines to the program between line 10 and 20 and line 40 and 50.



15 COLOR 4

45 COLOR 2



The number following the COLOR command may be any number 1 to 16. (16 is black and cannot be seen.) The numbers 17 through 31 may be used to change the color of what is being printed and to make it blink at the same time.











Activity 3



You can use the TAB function to make characters appear in different places on the screen. Type this line on the computer.



PRINT TAB(75);"* * *"



The asterisks should be at the right side of the screen. Now run this program.



ALT, F N

10 CLS

20 PRINT TAB(5);"your name"

30 PRINT TAB(10);"your name"

40 PRINT TAB(15);"your name"

50 PRINT TAB (20);"your name"

60 END



TAB moves characters right to the column number you request in parentheses, much like the tab on a typewriter. There are 80 spaces for characters across the screen and 25 spaces down.



LOCATE does much the same thing as TAB. The first number in a locate statement is the vertical distance (distance down), the second number is the horizontal distance (distance right). Run this program that uses LOCATE.



ALT, F N

10 CLS

20 LOCATE 10, 16

30 PRINT "STARS"

40 LOCATE 13, 9

50 PRINT "**** S T A R S ****"

60 LOCATE 17, 15

70 PRINT "HURRAY!"

90 END



Programming Tip: To save typing, you can use a colon to separate two or three short statements on one line. Use this shortcut sparingly, however. Although statements are executed more quickly, lines overloaded with statements are harder to read when looking for errors, and are often harder to correct. Also, if a line has a GOTO statement in it, the GOTO statement must be the last statement in a line; any statements following a GOTO will be ignored.



Now change and add these lines, then run the program again.

60 COLOR 0, 7 : REM INVERSE

70 PRINT "****"

80 COLOR 7, 0 : REM NORMAL

90 LOCATE 13, 15

100 PRINT "S T A R S"

110 COLOR 0, 7

120 LOCATE 13, 26

130 PRINT "****"

140 COLOR 23, 0 : REM FLASH

150 LOCATE 17, 17

160 PRINT "WOW!"

170 COLOR 7, 0

180 END

Along with the colors listed above, there are also many others. Here is a table to show what each does:



The first number in the COLOR statement tells the computer what color to make the letters. The number after the comma tells the computer what color to make the screen behind the letters.



You may assign a color to certain parts of text. For more information on color, see chapter 5, activity 2.

Write a program using any of the commands PRINT, GOTO, COLOR, TAB, and LOCATE. You may also want to use the comma and semicolon.



ALT, F N



10



20



30



40



50



60



70



80



90



100 END



Run your program on the computer.



2 Numbers

Activity 1



Use the PRINT command without quotation marks to tell the computer to perform math operations. Type these lines in and see the result.



PRINT 567

PRINT 56,789

PRINT 56789

PRINT -3.206

PRINT "5+3"

PRINT 5+3

PRINT 582.7+62

PRINT -5+9

PRINT 36.8-6.8

PRINT 8*7

PRINT 394*53

PRINT 42/6

PRINT 71/10



Write what each symbol tells the computer to do.



*



/



Why do you think the computer does not use an X or a dot to indicate multiplication?







The computer truncates (chops off) numbers to seven digits when it prints them. Type these lines.



PRINT 51/9

PRINT 62/11

PRINT 5/34

PRINT 1234567890



Write statements to calculate these expressions on the computer.



1. 18 - 9

2. 28,100 + 4,575



3. -5 times 3



4. 35 multiplied by itself



5. 98.38 x 30



6. 456 - 7



Type these problems and try to figure out the order in which the computer performs the math operations.



PRINT 8+4-2 PRINT 4*2+5*3

PRINT 8-4+2 PRINT 56/7/2+3*3

PRINT 8-4-2 PRINT 3+12/2-5

PRINT 8*4+2 PRINT 8*(4+2)

PRINT 8+4*2 PRINT (8-4)/2

PRINT 8-4*2 PRINT 8-(4*2)

PRINT 8/4+2 PRINT 4*(2+5)*3

PRINT 8+4/2 PRINT 6*(2*(2+3))

PRINT 8*4/2 PRINT 2*(6+(8-4)*2/4





Here are the computer's rules:



1. Operations in parentheses, if any, are done first,

starting with the innermost set of parentheses.

2. Multiplication and division are done next, from

left to right.

3. Then addition and subtraction are done, from left

to right.



Predict the output of these PRINT lines, using the rules listed above.



1. PRINT 6+5*2 5. PRINT 4*3+(5-1)



2. PRINT (6+5)*2 6. PRINT 3+9/3-2



3. PRINT 4*3+5-1 7. PRINT (3+9)/3-2



4. PRINT 4*(3+5)-1 8. PRINT (3+9/3)-2



Check your answers on the computer.



Make up some arithmetic expressions of your own. Predict the answers and check them on the computer.



1.



2.



3.



4.



5.



6.



7.



Activity 2



Type and run these programs on the computer.



ALT, F N

5 CLS

10 PRINT "28 + 4 = ";28+4

20 PRINT "28 - 4 = ";28-4

30 PRINT "28 * 4 = ";28*4

40 PRINT "28 / 4 = ";28/4

50 END



Programming Tip: In the above program, spaces were inserted around the numbers in the string but not around the numbers in the numeric expression after the semicolon. It is a typing shortcut to eliminate the spaces in the expression, but good programming practice to insert them around numbers that will appear on the screen for ease of reading and clarity.



ALT, F N

10 CLS

20 PRINT "14 DAYS = ";14 / 7;" WEEKS"

30 PRINT "35 DAYS = ";35 / 7;" WEEKS"

40 PRINT "309 DAYS = ";309 / 7" WEEKS"

50 END



ALT, F N

5 CLS

10 PRINT "JOSÉ'S SCORE",392

20 PRINT "LAURA'S SCORE",375

30 PRINT "KIM'S SCORE",286

40 PRINT," "

50 PRINT

60 PRINT "TOTAL SCORE",392+375+286

70 PRINT

80 PRINT "AVERAGE SCORE",(392+375+286)/3

90 END

ALT, F N

5 CLS

10 PRINT "NUMBER","SQUARE"

20 PRINT

30 PRINT 2, 2 * 2

40 PRINT 3, 3 * 3

50 PRINT 4, 4 * 4

60 PRINT 5, 5 * 5

70 END



Write a program that calculates and prints out how many seconds are in 1 day, 12 days, and 5,384 days.











Write a program that tells how much 324 doughnuts would cost if the price were $1.25 per dozen.











Run your programs on the computer.

Activity 3



To perform calculations using exponents, press the SHIFT key and 6 to make the ^ symbol. Type these print lines on the computer and write the output.



82 PRINT 8 ^ 2



73 PRINT 7 ^ 3

54 PRINT 5 ^ 4



35 PRINT 3 ^ 5



The INT function prints the nearest whole integer for a number in parentheses (but not greater than the number in parentheses). Type these lines and write the output.



PRINT INT(38) PRINT INT(-1.39)



PRINT INT(6.3248) PRINT INT(-.0005)



PRINT INT(1.39) PRINT INT(-.987)



PRINT INT(.347) PRINT INT(3+4.9)



PRINT INT(.0005) PRINT INT(3.3+4.9)



PRINT INT(.987) PRINT INT(5/2)



PRINT INT(-38) PRINT INT(1/3)

PRINT INT(-6.3248) PRINT INT(4-1.5)





Use the computer to figure out if these statements are true or false.



1. INT(3.6 +7.29) = INT(3.6) + INT(7.29)



2. INT(3.6 +7.42) = INT(3.6) + INT(7.42)



3. for any X and Y, INT(X+Y) = INT(X) + INT(Y)



4. 4*INT(3.24) = INT(4*3.24)



5. 4*INT(3.26) = INT(3.26)



The SQR function prints the square root of a positive number in parenthesis. Type these lines and write the output.



PRINT SQR(9)



PRINT SQR(100)



PRINT SQR(3.5)



PRINT SQR(.62)



PRINT SQR(2 * 6 + 26 / 2)



The ABS functions prints the absolute value (the distance it is from zero) of a number. Write the computer's output next to each line.



PRINT ABS(27)



PRINT ABS(.27)



PRINT ABS(0)



PRINT ABS(-.27)



PRINT ABS(5 * -5)

This program is a number guessing game. RND in line 50 stands for random and instructs the computer to choose a different answer each time you play the game. This is called a random result and can be compared to flipping a coin, rolling dice, turning a spinner, or dealing cards, and having no way of knowing what the result will be. You will learn about the LET, INPUT, and IF ... THEN statements in the next lesson. In line 1, RANDOMIZE TIMER means that the random numbers will all be completely randomly generated. RANDOMIZE TIMER should be used in all programs containing random number statements.



ALT, F N

1 RANDOMIZE TIMER

5 CLS

10 PRINT "I HAVE A NUMBER BETWEEN 1 AND 100."

20 PRINT "I WANT YOU TO GUESS IT."

30 PRINT "IF YOU DON'T GUESS IT."

40 PRINT "I WILL GIVE YOU A HINT."

50 LET N = INT(RND*100)+1

60 PRINT "YOUR GUESS";

70 INPUT X

80 IF X = N THEN GOTO 120

90 PRINT "THE DIFFERENCE = ",ABS(N-X)

100 PRINT "YOUR GUESS";

110 INPUT X

120 IF X = N THEN GOTO 150

130 PRINT "I WIN, MY NUMBER = ";N:PRINT

140 GOTO 160

150 PRINT "YOU WIN!":PRINT

160 PRINT "TO START AGAIN, TYPE SHIFT F5."

170 END



Run the program. Try it out on a friend.



3 Variables



Activity 1



A variable is a name of a certain storage location in the computer's memory that can be assigned a value with a LET statement. In a LET statement, the equal sign means stand for or replace, rather than strictly same as. This value can be changed with another LET statement.



Run this program on the computer and write the value of numeric variable S in the computer's memory for each line.



ALT, F N



10 LET S = 4



20 LET S = 6 * 3



30 LET S = S + 2



40 LET S = S / 2 + S



50 PRINT S



60 END



Programming Tip: Numeric variable names must begin with a letter. Try to make your variables descriptive, because it will make them easier to remember. Also, you cannot use certain reserved words as a variable name or within a variable name, or you will get a SYNTAX ERROR message. These words are commands and function names such as COLOR, ABS, LET, IF, and FOR.

The value of a numeric variable stays the same until you give it a new value. What will be the value of each variable after each line is executed?



1. LET A = 10 A =



2. LET B = 5 A = B =



3. LET C = A + B A = B = C =



4. LET B = 7 A = B = C =



5. LET C = C - B A = B = C =



6. LET A = 2 A = B = C =



7. LET D = C - A A = B = C = D =



8. LET A = A - D A = B = C = D =







Type and run these programs on the computer:



ALT, F N

5 CLS

10 LET Y = 6

20 PRINT Y, Y + 2, Y - 2

30 PRINT Y * 2, Y / 2, Y ^ 2

40 END



ALT, F N

5 CLS

10 LET P = 3

20 LET O = P * 16

30 PRINT P; " POUNDS = "; O; " OUNCES"

40 END





ALT, F N

10 CLS

20 FEET = 5

20 LET FEET; " FEET = ";

30 PRINT FEET; " FEET = ";

40 PRINT FEET * 12; " INCHES"

50 END



Type and run this program that uses LET with GOTO to tell the computer to count.



ALT, F N

10 CLS

20 LET N = 0

30 PRINT N

40 LET N = N + 1

50 GOTO 30

60 END



Use CTRL-BREAK to stop the counting. To tell the computer to start counting at 50, change line 20 and run the program.



20 LET N = 50



To count by even numbers, change line 40.



40 LET N = N + 2



You can use IF and THEN to tell the computer how far to count. Add this line to your program.



45 IF N > 100 THEN END

To count backwards, use a minus sign in lines 40 and 45.



40 LET N = N - 5

45 IF N < -100 THEN END



Programming Tip: When you tell the computer to end in an IF ... THEN statement, no separate END statement is required. It is good programming practice to have only one END statement per program.



Write a program to count by odd numbers from 1 to 11.









Write a program to count by fives from 50 to 500.











Write a program that will print the perimeter and the area of a rectangle that is 5 inches long and 2 inches wide. (Remember that perimeter P = 2 x L + 2 x W and area A = L x W.)









Activity 2



The INPUT command lets you "talk" with the computer. It prints a question mark on the screen and tells the program to wait for you to type something. Type and run these programs that use INPUT.



ALT, F N

10 CLS

20 PRINT "HOW MANY MILES"

30 INPUT M

40 PRINT

50 PRINT M; "MILES = ";

60 PRINT M * 5280; " FEET"

70 END



Insert these lines to the program and run it.



70 PRINT

80 GOTO 20

90 END



Use CTRL-BREAK to stop the program. Now type and run these programs.



ALT, F N

10 CLS

20 PRINT "LENGTH IS";

30 INPUT L

40 PRINT "WIDTH IS";

50 INPUT W

60 PRINT "PERIMETER IS "; 2 * L + 2 * W

70 PRINT "AREA IS "; L * W

80 PRINT

90 GOTO 20

100 END





ALT, F N

10 CLS

20 PRINT "ENTER THREE SCORES,";

30 PRINT "SEPARATED BY COMMAS."

40 INPUT S1, S2, S3

50 PRINT

60 PRINT "TOTAL SCORE IS "; S1 + S2 + S3

70 PRINT "AVERAGE SCORE IS";

80 PRINT (S1 + S2 + S3) / 3

90 PRINT

100 GOTO 20

110 END

Suppose that newspapers cost carriers $0.175 each and they sell them for $.2 each. Write a program that asks the carriers how many papers they want and then prints the carrier cost and profit for that amount.









You can use a numeric variable to keep track of totals in a program. These are called running or cumulative totals. Type and run these programs on the computer.



ALT, F N

10 CLS

20 LET TS = 0

30 PRINT "YOUR SCORE";

40 INPUT S

50 LET TS = TS + S

60 PRINT "TEAM SCORE IS NOW"; TS

70 PRINT

80 GOTO 30

90 END

ALT, F N

10 CLS

20 LET TC = 0

30 PRINT "HOW MANY ITEMS"';

40 INPUT I

50 PRINT "PRICE PER ITEM";

60 INPUT P

70 LET C = I * P

80 LET TC = TC + C

90 PRINT "COST IS $"; C, "TOTAL IS $"; TC

100 PRINT

110 GOTO 30

120 END



Try to eliminate unnecessary variables from programs. To eliminate variable C in the program above, delete line 70 and change lines 80 and 90.



80 LET TC = TC + I * P

90 PRINT "COST IS $"; I * P, "TOTAL IS $"; TC



Programming Tip: To delete lines, move over them with the cursor while pressing the shift key. Then press the delete key.



Write your own program that asks for the input of several numbers and keeps a cumulative total.











Run your program. Try it out on a friend.

You can use IF... THEN in an input program. Type and run these programs.



ALT, F N

5 CLS

10 PRINT "HOW MANY INCHES";

20 INPUT I

30 IF I < 36 THEN PRINT I; " INCHES IS LESS THAN A YARD."

40 IF I = 36 THEN PRINT I; " INCHES EQUALS ONE YARD."

50 IF I > 36 THEN PRINT I; " INCHES IS MORE THAN A YARD."

60 GOTO 10

70 END



ALT, F N

5 CLS

10 PRINT "* BINGO GAME *"

20 PRINT: PRINT "TYPE A NUMBER."

30 PRINT: INPUT N

40 IF N > 50 THEN GOTO 70

50 IF N < 50 THEN GOTO 80

60 IF N = 50 THEN GOTO 90

70 PRINT "TRY A SMALLER NUMBER.": GOTO 30

80 PRINT "TRY A LARGER NUMBER.": GOTO 30

90 PRINT "BINGO!"

100 END



In line 60 below, <> means not equal to. Remember that a colon (:) lets you type another command on the same line.



ALT, F N

10 CLS

20 PRINT "I'M THINKING OF A NUMBER"

30 PRINT "BETWEEN 1 AND 10."

40 PRINT "GUESS MY NUMBER."

50 INPUT Q

60 IF Q <> 7 THEN PRINT "TRY AGAIN.": GOTO 40

70 PRINT "YOU GUESSED IT!"

80 END



In Qbasic, you can use a second type of IF statement called a Block IF statement. You may put as many lines as you like in a Block IF statement.



ALT, F N

10 CLS

20 PRINT "I'M THINKING OF A NUMBER"

30 PRINT "BETWEEN 1 AND 10"

40 PRINT "GUESS MY NUMBER"

50 INPUT Q

60 IF Q = 7 THEN

70 PRINT "YOU GUESSED IT"

80 PRINT "YOU WIN"

90 END

100 ELSE

110 PRINT "NO, TRY AGAIN"

120 ENDIF

130 END



The Block IF statement will keep going until it sees an ENDIF statement. An ELSE command can be put in to tell the computer what to do when the IF statement is not true.



See if you can change this program so the number to guess comes from an INPUT statement. Be sure to have the computer clear the screen using CLS before asking the person playing the game to start guessing.





















Programming Tip: Good programmers make their programs easier to read using indentation and blank spaces. The statements inside an IF statement are usually indented.

In the program below, the variable C is a counter, which keeps track of how many times the loop has been repeated. Because line 60 tells the program to end, a separate END statement is not needed.



ALT, F N

10 CLS

20 LET C = 0

30 PRINT "THIS IS A"

40 PRINT "GOTO LOOP."

50 LET C = C + 1

60 IF C > 10 THEN END

70 GOTO 30



You can type PRINT C to see the value of C after the program has been run. You can make this program loop 20 times. Change the counter C in line 60 to this, run the program, then type PRINT C.



60 IF C > 20 THEN END



Type and run this program on the computer.



ALT, F N

5 CLS

10 PRINT "* MULTIPLE CHOICE *"

20 PRINT "CHOOSE SOMETHING THAT SWIMS"

30 PRINT

40 PRINT " 1) POLEMONIUM"

50 PRINT " 2) ERITHACUS RUBECOLA"

60 PRINT " 3) CETACEA"

70 INPUT A

80 IF A = 1 THEN

90 PRINT "SORRY, BUT THAT'S A FLOWER"

100 ELSEIF A = 2 THEN

110 PRINT "SORRY, BUT THAT'S A ROBIN."

120 ELSEIF A = 3 THEN

130 PRINT "CONGRATULATIONS, THAT'S A WHALE!"

140 ELSE

150 PRINT "YOU NEED TO TYPE 1, 2, OR 3."

160 GOTO 70

170 ENDIF

180 END



Notice that in this program, if the user types in any number except 1, 2 or 3 the computer will ask them to retype their answer.



Programming Tip: It is a good idea to make your programs easy to use. Try to let the person running the program fix any typing mistakes they make. Have the computer re-ask questions when what they typed doesn't make sense. Programs that do this are called Robust.



You can simplify this program by using the SELECT CASE command.



ALT, F N

5 CLS

10 PRINT "* MULTIPLE CHOICE *"

20 PRINT "CHOOSE SOMETHING THAT SWIMS"

30 PRINT

40 PRINT " 1) POLEMONIUM"

50 PRINT " 2) ERITHACUS RUBECOLA"

60 PRINT " 3) CETACEA"

70 INPUT A

80 SELECT CASE A

CASE 1

PRINT "SORRY, BUT THAT'S A FLOWER"

CASE 2

PRINT "SORRY, BUT THAT'S A ROBIN."

CASE 3

PRINT "CONGRATULATIONS, THAT'S A WHALE!"

CASE ELSE

PRINT "YOU NEED TO TYPE 1, 2, OR 3."

GOTO 70

END SELECT

190 END





Try this program out on a friend.



The SELECT CASE command has some really nice options. For example:





CASE Command When it is true



CASE 1, 3, 5, 7 For numbers 1, 3, 5, 7



CASE 1 TO 100 For numbers 1 to 100



CASE 1, 2, Var1, Var2 For 1 and 2 and what ever Var1 and

Var2 happen to equal.



CASE IS < 5 For numbers less than 5



CASE IS > 200 For numbers greater than 200



CASE "A" TO "Z" For any letter A through Z



Activity 3



You know that GOTO can make a loop in a program. The words FOR and NEXT can also make a loop. The computer executes the statements between the FOR and NEXT statements as many times as the FOR statement allows. Here is another way to count to 20 on the computer. Compare it to the counting programs that use LET and GOTO in Activity 1.

ALT, F N

5 CLS

10 FOR X = 0 TO 20

20 PRINT X

30 NEXT X

40 END



Run the program. Change line 10 to count by twos.



10 FOR X = 0 TO 20 STEP 2



The STEP command tells the computer to count by twos.

Run the program. Change line 10 to this.



10 FOR X = 0 TO 20 STEP 3.25



Run the program then change line 10 to count to 20 by fives.



10



Count by threes to 60.



10



Change line 10 to this and try it.

10 FOR X = 100 TO 5 STEP -5

Can you count by twos backwards from 30 to -30?



10



This program has three FOR ... NEXT loops with three different variables: A, B, and C. Each loop is executed 100 times.



ALT, F N

5 CLS

10 PRINT "* CONTINUING MESSAGE *"

20 FOR A = 1 TO 100

30 PRINT "YOU "

40 NEXT A

50 FOR B = 1 TO 100

60 PRINT "ARE";

70 NEXT B

80 FOR C = 1 TO 100

90 PRINT "W O N D E R F U L !"

100 NEXT C

110 END



This program counts as far as you tell it.



ALT, F N

5 CLS

10 PRINT "COUNT HOW FAR";

20 INPUT A

30 FOR X = 1 TO A

40 PRINT X

50 NEXT X

60 END





Use CTRL-BREAK and press to stop. The next program asks you to tell it where to start counting and where to stop.



ALT, F N

5 CLS

10 PRINT "BEGINNING NUMBER";

20 INPUT B

30 PRINT "ENDING NUMBER";

40 INPUT E

50 FOR N = B TO E

60 PRINT N

70 NEXT N

80 GOTO 10

90 END



A nested loop is a FOR ... NEXT loop inside another FOR ... NEXT loop. In the program below the loop with the variable K is inside the loop with the variable N.



ALT, F N

5 CLS

10 FOR N = 1 TO 20

20 FOR K = 1 TO N

30 PRINT "*";

40 NEXT K

50 PRINT

60 NEXT N

70 GOTO 10

80 END



This program has two loops nested inside a larger loop.



ALT, F N

5 CLS

10 FOR L = 1 TO 10

20 FOR X = 1 TO L

30 PRINT "X";

40 NEXT X

50 PRINT "";

60 FOR Y = 1 TO 11- L

70 PRINT "O";

80 NEXT Y

90 PRINT

100 NEXT L

110 GOTO 10

120 END



Programming Tip: FOR ... NEXT loops may be nested, but may not cross. It is a good idea to draw arrows, if only imaginary ones, to make sure no lines cross. Below is an example of loops that are not nested properly:



ALT, F N

10 FOR N = 10 TO 20

20 PRINT N

30 FOR I = 30 TO 40

40 PRINT I

50 NEXT N

60 NEXT I

70 END



To uncross the loops, switch lines 50 and 60.



Activity 4



Type and run this program to print 20 integers chosen at random from the values 0 to 9.



ALT, F N

5 CLS

10 FOR N = 1 TO 20

20 LET Z = INT(RND * 10)

30 PRINT Z

40 NEXT N

50 END



Line 10 tells the computer to print 20 numbers. Line 20 says those numbers should be 10 random integers, starting with zero, or 0 to 9. Change line 20 to this.



20 LET Z = INT(RND * 11) + 1



Run the program. Line 20 now tells the computer to print 11 random integers, starting with 1, or 1 to 11. The +1 tells the computer at which number to start. In the first program above, +0 was understood. Change line 20 to each line below, run the program, and write what random integers are generated.



20 LET Z = INT(RND * 5)



20 LET Z = INT(RND * 15) + 10



20 LET Z = INT(RND * 4) - 3



Write line 20 to generate random integers:



1. from 23 through 45



2. from -13 through 0



3. from -5 through -19



Give the lowest and highest possible values of X.



1. LET X = INT(RND * 35) + 2



2. LET X = INT(RND * 10) - 43



3. LET X = INT(RND * 59) - 15



4. LET X = INT(RND * 13) - 97



5. LET X = INT(RND * 21) - 10



Type this program, which is a simulation of the random results of throwing dice.



ALT, F N

10 CLS

20 LET X = INT(RND * 6) + 1

30 LET Y = INT(RND * 6) + 1

40 PRINT "DIE 1 = "; X

50 PRINT "DIE 2 = "; Y

60 PRINT "TOTAL = "; X + Y

70 END



Run the program several times to see the random results.



This program is a simulation of tossing a coin. The

computer will print twos to represent heads and ones to

represent tails.



ALT, F N

10 CLS

20 PRINT "NUMBER OF HEADS/TAILS"

30 PRINT "RANDOMLY GENERATED"

40 PRINT "FROM 100 COIN TOSSES."

50 LET T = O: LET H = O

60 FOR I = 1 TO 100

70 LET C = INT(RND * 2) + 1

80 PRINT C

90 IF C = 1 THEN T = T + 1: GOTO 110

100 LET H = H + 1

110 NEXT I

120 PRINT "HEADS = "; H

130 PRINT "TAILS = "; T



This program asks the user to supply a missing number in a subtraction problem.



ALT, F N

5 CLS

10 PRINT "MISSING NUMBER QUIZ"

20 PRINT

30 PRINT "WHAT NUMBER IS MISSING?"

40 LET A = INT(RND * 11) + 1

50 LET B = INT(RND * 20) + 10

60 PRINT B; " - ? = "; A

70 INPUT G

80 IF B = A + G THEN GOTO 100

90 PRINT "TRY AGAIN.": GOTO 60

100 PRINT "EXACTLY RIGHT!"

110 END



Run the programs. Write a program to ask random multiplication questions. Make it as fancy as you can.



Activity 5



You know that characters and spaces inside a set of quotation marks are called a string. A variable that stands for a string of letters, numbers, symbols, and spaces is called a string variable. It looks the same as a numeric variable except that it has a dollar sign ($) in the label. Run this program that shows what the word BASIC (the name of the language in which you are programming) means.



ALT, F N

10 CLS

20 LET B$ = "BEGINNER'S": PRINT B$

30 LET A$ = "ALL-PURPOSE": PRINT A$

40 LET S$ = "SYMBOLIC": PRINT S$

50 LET I$ = "INSTRUCTION": PRINT I$

60 LET C$ = "CODE": PRINT C$

70 END



LEN stands for length and finds the number of characters (including spaces) in a string variable. Type and run this program on the computer.



ALT, F N

10 CLS

20 PRINT "TYPE ANY WORD."

30 INPUT A$

40 PRINT "THE WORD HAS "; LEN(A$);

50 PRINT "LETTERS IN IT."

60 END



Each time you run the program the computer will tell you the number of letters of letters in the word you type as input.





LEFT$(A$,1) will tell you the first letter of a string variable. The program below shows how LEN and LEFT$ can be used.



ALT, F N

10 CLS

20 PRINT "HOW MANY SPELLING WORDS DO YOU WANT"

30 PRINT "TO PRACTICE?"

40 PRINT "TYPE A NUMBER FROM 1 TO 10."

50 INPUT N

60 FOR I = 1 TO N

70 CLS

80 PRINT "TYPE A WORD."

90 INPUT A$(I)

100 PRINT "TYPE A WORD THAT RHYMES WITH YOUR WORD."

110 INPUT B$(I)

120 CLS

130 NEXT I

140 PRINT "NOW LET'S PRACTICE SPELLING."

150 PRINT " ***************************"

160 PRINT

170 FOR X = 1 TO N

180 PRINT "THE WORD BEGINS WITH "; LEFT$(A$(X), 1)"."

190 PRINT

200 PRINT "IT HAS"; LEN(A$(X); " LETTERS."

210 PRINT

220 PRINT "IT RHYMES WITH "; B$(X)"."

230 PRINT

240 PRINT "TYPE THE WORD."

250 INPUT C$:PRINT:PRINT

260 IF C$<>A$(X) THEN PRINT "TRY AGAIN.":GOTO 180

270 PRINT "RIGHT!"

280 NEXT X

290 END



Try this program out on a friend.



In A$(X) the variable X is called a subscript. If there is more than one string assigned to a variable like A$, it tells the computer which one you want to use in each statement. A$(1) would be the first one. A$(2) would be the second, and so on. When the subscript itself is a variable, like X in our example, when the subscript changes the computer uses different strings stored in A$.



LEFT$(A$,2) will tell you the first two letters of a string variable, and so on. RIGHT$ does much the same thing as LEFT$. Type and run this program, using your first and last names.



ALT, F N

5 CLS

10 LET A$ = "your name"

20 FOR N = 1 TO LEN(A$)

30 PRINT LEFT$(A$, N)

40 NEXT N

50 END













4 Saving and Loading



Activity 1



When you turn off the computer and park the hard disk, the programs you have written are erased from the computer's memory. You can save the programs you write on a diskette and see them again and again.



When you see the cursor, type and run this program on the computer.



ALT, F N

5 CLS

10 PRINT "* ADDING MACHINE *": PRINT

20 PRINT "WHAT IS X";

30 INPUT X

40 PRINT "X + 2X + 3X = ";

50 PRINT X + 2 * X + 3 * X

60 END



The command ALT F A tells the computer to save a program for the first time. To Save it on drive A, your disk, as ADD. Type A:ADD and press . The light on the disk drive will come on for a moment. When it goes off, the program will be saved.



Programming Tip: Program names must start with a letter and can be up to 8 characters long. You can use numbers after the first letter, but not punctuation or spaces. Keep program names short but descriptive.



Now type and run this program.



ALT, F N

10 CLS

20 PRINT "H H III"

30 PRINT "H H I"

40 PRINT "HHHH I"

50 PRINT "H H I"

60 PRINT "H H III"

70 END



Give this program the name HI and save it on the diskette.



To ask the computer to show you what programs are on your diskette, Type ALT F O, A:. You will see a list of the programs you just saved.



To run the program you named ADD, type ALT F O A:ADD and press . The red light on the disk drive will glow as the computer searches the diskette for a program named ADD. Then type SHIFT F5 and the program will run on the screen.



Write a short program to save on the diskette.





Follow these steps:



1. Name the program and save it on the diskette.

2. Type ALT, F N to erase the program from the

computer's memory.

3. Load the program from the diskette.

4. Run the program.

Activity 2



If you want to change a line in a program, erase the old line and type the new one in its place. Load the ADD program from your diskette. Change the value of X in lines 40 and 50. You now have a new program. Save it with the name ADD2, then type ALT, F O A: to see your list of programs. Not only is ADD still on the list, but also the new ADD2 program.



As you work in this book, you may want to save some of the programs. Be sure to put a formatted diskette in the drive each time you work at the computer so that you can save, load, and run those programs any time without having to type them again.



Write what each command tells the computer to do.



ALT F S





ALT F O A:





ALT F O





ALT F N





5 Graphics



Activity 1



In QBasic, there are many ways of creating graphics programs. In this lesson, we are going to use screen 12, High Resolution (clarity) VGA graphics. Using the SCREEN 12 command makes the monitor show 640 pixels (dots) horizontally (across) by 480 vertically (down). Try the following program.



ALT F N

10 SCREEN 12

20 COLOR 1

30 LINE (0,0)-(639,0)

40 LINE (639,0)-(639,479)

50 LINE (639,479)-(0,479)

60 LINE (0,479)-(0,0)



What does this program do?





The screen is set up like a grid. Along the top of the screen, 0 is the left of the screen, and 639 is the right. To the side, 0 is the top, and 479 is the bottom. The LINE command takes two points on the screen and draws a line between them. A point is the location of a single pixel.

To tell the computer which pixel you want, you tell it how far to travel from the right top corner of the screen. In the LINE command, the first number in parentheses tells the computer how far to the right to travel from the corner to get to the point. The second number in parentheses tells the computer how far down from the top right corner you have to travel to get to the point.



LINE (How far right , How far down) - (How far right , How far down )

Beginning of line End of line



Every LINE command needs to have the location of two points, the place to begin drawing the line and the place to stop drawing the line.



LINE (10,15)-(300,400), simply means draw a line from location 10,15 to location 300,400. And LINE (320,240)-(320,240) would make a dot in the middle of the screen.



Write a program to draw a line from the top left corner of the screen to the bottom right corner of the screen. Remember to set the screen to 12 and put in a COLOR command with a number between 1 and 15.









Now write a program to write the first letter of your name.

















Activity 2



Run this program to see all the colors SCREEN 12 can make.



10 SCREEN 12

20 FOR X = 1 TO 15

30 COLOR X

40 LINE (100,X)-(200,X)

50 NEXT X



You will notice that there are several different colors available to you. Run this program to see each of the colors and their numbers.



10 SCREEN 12

20 PRINT "WHAT COLOR NUMBER (0-15)->";

30 INPUT C

40 COLOR C

50 LINE (100,100)-(300,300)

60 GOTO 20



Here is a list of the colors and their numbers for SCREEN 12:



0 = Black 8 = Dark Grey

1 = Dark Blue 9 = High Dark Blue

2 = Green 10 = High Green

3 = Light Blue 11 = High Light Blue

4 = Red 12 = High Red

5 = Purple 13 = High Purple

6 = Brown 14 = Yellow

7 = Light Grey 15 = White





Run this program to draw a truck.



10 SCREEN 12

20 COLOR 3

30 LINE (100,100)-(400,100)

40 LINE (400,100)-(400,300)

50 LINE (100,100)-(100,300)

60 LINE (400,300)-(100,300)

70 LINE (400,200)-(470,200)

80 LINE (470,200)-(470,300)

90 LINE (400,300)-(470,300)

100 LINE (100,300)-(120,330)

110 LINE (120,330)-(150,330)

120 LINE (150,300)-(150,330)

130 LINE (300,300)-(300,330)

140 LINE (300,330)-(330,330)

150 LINE (330,300)-(330,330)

Boxes: In the previous programs, you used the line command to draw pictures and letters. If you used the line command in this way, it would take four separate line commands to draw a box. Fortunately, there is a much easier way. There is a special option in the line command just for drawing boxes. The command LINE (100,100)-(300,300),,B draws a box with an upper left hand corner at location 100,100 and a lower right hand corner at location 300,300. The place between the two commas is the place you could put the color number, if you wanted the box to be a different color from the drawings you made earlier.



Try this program to see how easy it is to make boxes:



10 SCREEN 12

20 COLOR 4

30 LINE (20,20)-(50,50),,B

40 LINE (60,60)-(200,100),,B

50 LINE (10,10)-(500,400),,B

60 END







Try this program to see boxes in color:



10 SCREEN 12

20 FOR X= 1 TO 15

30 COLOR X

40 LINE (1,1)-(X*10,X*10),,B

50 NEXT X

60 END



The color changed for each box and the bottom corner moved down at an angle for each box. This program could be made one line shorter by taking out the COLOR command and changing the line command to:



LINE (1,1)-(X*10,X*10),X,B



Filling Boxes: QBasic has an option to fill boxes with color. If you place the letters BF at the end of a line command, QBasic will draw a box and fill it up.



Type this program in and try it out.



10 SCREEN 12

20 LINE (1,1)-(50,50),3,BF

30 LINE (30,30)-(100,100),5,BF

40 LINE (70,70)-(200,200),7,BF

50 LINE (180,180)-(350,350),9,BF

60 END



Notice how each box "covers over" the box draw before it.



Programming Tip: Objects drawn first tend to look like they are in the background or behind everything else. Recently drawn objects look like they are in front or on top of earlier pictures. Good programmers handle the background of their graphics work first. They then program objects to appear on top of the backgrounds they created earlier.











Write a program to draw the following shape:















Now Write a program that draws a house.















Activity 3



There are several other commands that will help you if you want to write more advance programs.



CIRCLE, not surprisingly, is a command that gives the ability to the user to draw a circle.



Here is a program to draw two circles on the screen.



5 SCREEN 12: COLOR 3

10 CIRCLE (200,200), 100

20 CIRCLE (100,100), 50



The location 200,200 is the center of a circle. 100 is the radius (how far out from the center to draw the circle.)



Type in and run this program.



10 SCREEN 12

20 COLOR 9

30 LINE (100,100)-(200,200),,B

40 COLOR 13

50 LINE (300,300)-(500,300)

60 LINE (500,300)-(250,200)

70 LINE (250,200)-(300,300)

80 COLOR 12

90 CIRCLE (100,300),60



As you can see, a series of shapes appears on the screen. Now add these lines to the program.



130 PAINT (150,150),10,9

140 PAINT (350,250),11,13

150 PAINT (100,300),14,12



In line 130, PAINT (150,150) tells the computer to paint all the pixels on the screen around location 150,150 until another line is encountered. The number 10 is the color of the area is painted, and 9 tells the computer to stop painting when it reaches any boundaries of that color.



Now write a program to draw a truck that is filled in with color and also has round wheels.





















Activity 4



Run this program to make an object move across the screen.



10 SCREEN 12

20 FOR X = 1 TO 500

30 COLOR 3

40 LINE (X,200)-(X+100,200)

50 LINE (X,200)-(X,400)

60 LINE (X,300)-(X+100,300)

70 LINE (X+100,200)-(X+100,300)

80 COLOR 0

90 LINE (X,200)-(X+100,200)

100 LINE (X,200)-(X,400)

110 LINE (X,300)-(X+100,300)

120 LINE (X+100,200)-(X+100,300)

130 NEXT X



Save this program on your diskette. Try to make the object larger, change the color. How does your computer make the object seem to move? (Hint: look at the color chart for color number 0)







Programs that make things move are called animation programs. Run your animation program. Think of ways to modify it. Some ideas for programs might be a car that drives across the screen, a dog that runs, a flower that grows.





Another way to put animation in programs is to photograph sections of the screen in large variables called arrays and then make them appear in different places on the screen.



Type in this program and try it out.



10 DIM A(100)

20 SCREEN 12

30 CIRCLE (10,100),8

40 CIRCLE (7,100),6

50 CIRCLE(4,100),3

60 GET (1,90)-(10,110),A

70 X = 1

80 DO WHILE X < 600

90 PUT (X,90), A, XOR

100 X = X + 1

110 PUT (X,90), A, XOR

120 FOR DELAY = 1 TO 100: NEXT DELAY

130 LOOP



The GET command "takes a picture" of a box-shaped section of the screen and stores it in a variable named A. The PUT command takes the picture out of variable A and puts it on the screen so its left corner begins at the point the numbers in the parentheses indicate.



One great advantage to programming animation in this way is that backgrounds are left intact. As the object passes across the screen, the XOR command in the PUT statement puts the background back in its place.



Add these lines to your program to see the program handle background.



52 LINE (100,1)-(150,400),,B

54 LINE (120,1)-(200,400),,B





















Change this program so that a flower travels across the screen.





































6 Debugging



Activity 1



No program is ever really "wrong"--it just may have a few bugs in it. A bug is a mistake in a program and debugging is what you do to fix that mistake. When you corrected typing errors as you worked through this book, you were debugging your programs.



There are two kinds of bugs. A program with a mistake in logic will run but you won't get the result you wanted. A program with a mistake in syntax will not run and the computer will display an error message on the screen. Always check for spelling and typing errors first. The computer will often tell you which line has the error if it is an error in syntax.



Each of the programs below has a bug. Type and run the program as it is to see if it does what it is supposed to do. Then find the bug and run the program with the new line. You may not always fix the bug the first time you try. When you are sure you have solved the problem, write the new line or lines next to the program line or lines in error. Then decide whether it was an error in logic or syntax and put an X in the appropriate blank.



This program should print names of musical instruments in columns on the screen.



ALT, F N

10 CLS

20 PRINT "FLUTES",

30 PRINT "OBOES",

40 PRINT CORNETS"

50 GOTO 20

60 END



logic syntax



This program should fill the screen with names of musical instruments.



ALT, F N

10 CLS

20 PRINT "FLUTES";

30 PRINT "OBOES";

40 PRINT "CORNETS";

50 PRINT "DRUMS";

60 PRINT "TROMBONES";

70 PRINT "CLARINETS";

80 GOTO 10

90 END



logic syntax



This program should print words in a diagonal line on the screen.



ALT, F N

10 CLS

20 PRINT TAB(5); "BEGINNER'S"

30 PRINT TAB(9); "ALL-PURPOSE"

40 PRINT TAB(13); "SYMBOLIC"

50 PRINT TAB(17); "INSTRUCTION"

60 PRINT TAB(21); "CODE"

70 END



logic syntax



This program should print the answers to three math problems.



ALT, F N

10 CLS

20 PRINT "6 + 5 * 2"

30 PRINT "(3 + 9) / 3 - 2"

40 PRINT "5 * 5 - 5"



logic syntax



This program should count by threes.



ALT, F N

10 CLS

20 LET N = 0

30 PRINT N

40 LET N = N + 3

50 GOTO 30

60 END



logic syntax



This program should count by odd numbers from 1 to 21.



ALT, F N

5 CLS

10 LET N = 1

20 LET N = N = 1

30 PRINT N

40 IF N = 21 THEN END

50 GOTO 30

60 END



logic syntax





Activity 2



This program should convert inches to feet.



ALT, F N

10 CLS

20 PRINT "HOW MANY INCHES"

30 INPUT I

40 PRINT

50 PRINT F;" INCHES = ";

60 PRINT F / 12;" FEET"

70 END



logic syntax



ALT, F N

10 CLS

20 PRINT "WHAT IS MY NUMBER (1-15)";

30 INPUT N

40 IF N <> 7 THEN PRINT "TRY AGAIN.";GOTO 50

50 PRINT "YOU GUESSED IT!"

60 END



logic syntax



This program should draw a square.



ALT, F N

10 SCREEN 12

20 COLOR 3

30 LINE (100,100)-(200,100)

40 LINE (200,100)-(200,200)

50 LINE (200,200)-(100,200)

60 LINE (100,100)-(100,100)

70 END



logic syntax



This program should print a vertical line across the screen.



ALT, F N

10 SCREEN 12

20 COLOR 3

30 LINE (0,300)-(300,300)

40 END



logic syntax



This program should show all the colors on the computer.



ALT, F N

5 SCREEN 12

10 CLS

20 FOR C = 1 TO 15

30 COLOR C

40 LINE (100,100)-(200,200)

50 C

60 END



logic syntax



This program should ask for a color number from 1 to 15 and draw a box that color.



10 SCREEN 12

20 PRINT "WHAT COLOR IS THE BOX (1-15)->";

30 INPUT

40 COLOR C

50 LINE (100,100)-(200,100)

60 LINE (200,100)-(200,200)

70 LINE (200,200)-(100,200)

80 LINE (100,200)-(100,100)

90 END



logic syntax



Activity 3



Here are some programs with several bugs in them. Study each one carefully, then write the new lines to the side of each line with a bug. Then run the program to see if you caught all the bugs and debugged them correctly.



This input program should make a silly sentence.



ALT, F N

10 CLS

20 PRINT "NAME A FRIEND."

30 INPUT F$

40 PRINT NAME A COLOR."

50 INPUT C$

60 "NAME AN ANIMAL."

70 INPUT A

80 PRINT

90 PRINT F$;" AND I RODE TO SCHOOL"

100 PRINT "ON A ";C$" ";A"."

110 END



This program should ask for two numbers, then

multiply them.



ALT, F N

5 CLS

10 PRINT * MAGIC MULTIPLYING MACHINE *

20 PRINT:PRINT "FIRST NUMBER";

30 INPUT X

40 PRINT "SECOND NUMBER",

50 INPUT Y

60 PRINT

70 PRINT X + Y

80 PRINT

90 GOTO 40

100 END



This program should print a total of scores entered by the user.



ALT, F N

10 CLS

20 LET T = 0

30 PRINT "I WILL ADD YOUR SCORES."

40 PRINT "ENTER O WHEN DONE."

50 PRINT

60 PRINT "SCORE";INPUT S

70 IF S = 0 GOTO 60

80 LET T = T + S

90 GOTO 50

100 PRINT:PRINT "TOTAL = ";S

110 END



This program should print a word within a border of flashing asterisks. Line 20 has 39 asterisks.



ALT, F N

10 CLS

20 A$="***************************************"

30 COLOR 23

40 PRINT A

50 FOR I=1 TO 20

60 PRINT "*"

70 NEXT 1

80 LOCATE 39, 2

90 FOR J=1 TO 20

100 LOCATE 39

110 PRINT "*"

120 NEXT J

130 LOCATE 0, 22

140 PRINT A

150 COLOR 7

160 LOCATE 18, 10

170 PRINT "WOW!"

180 GOTO 180

7 Added Features



Activity 1



When you write and execute a program, you are teaching the computer to do something. The computer can organize information for you and perform calculations rapidly, but you must tell it what you want it to do in a logical, orderly way.



Suppose you need to keep track of books in a collection. You want to use a computer because you want to:



1. get a printout on the screen at any time of the

books in the collection.

2. add books to the collection.

3. delete books from the collection.

4. change information about books in the collection.



Using your knowledge of what computers can do, what input would you have to provide for the computer to perform the steps listed above?









List three ideas of possible programs to teach the computer.



1.



2.



3.





Now pick one of those ideas and break it down into

the steps you would need to do to accomplish it.



1.



2.



3.



4.



5.



6.



7.



8.



9.



10.



Flowcharting is one way of diagramming the major steps in a computer program. It helps you to organize your ideas before writing your program on the computer. Below are the common symbols used when making a flow chart.



Start/stop box indicates the official beginning and end of a flow chart



Operation box contains something that is to be done (for example, count to 10)



Input/output box used when input is requested from the keyboard (or joystick/mouse) or when the computer is to print out something



Decision box contains a question that has only a yes or no answer to it (for example, Does T=100?)



Arrow connects the boxes to show what paths the program can take



Trace through this flow chart of a program that teaches the computer to print even numbers from 0 to 100. Write the program it represents on the lines beside it.













Draw your own flow chart to print all the numbers backwards from 100 to 0. Use the space to the right of the program.















One box in a flow chart can represent one or more program steps, as shown below.



ALT, F N

10 CLS:LET PROBLEM = 0

20 LET A = INT(RND * 100)

30 LET B = INT(RND * 100)

40 PRINT A;" + ";B;" = ";

50 INPUT ANSWER

60 IF ANSWER = A + B THEN GOTO 80

70 PRINT "SORRY, TRY AGAIN.": GOTO 40

80 LET PROBLEM = PROBLEM + 1

90 IF PROBLEM < 10 THEN GOTO 20

100 PRINT "THAT'S ALL FOR TODAY."



Write your own flow chart and program to give a ten-item multiplication quiz. Then type and run your program to see if it works.

Activity 2



Many of the programs you have seen thus far are short routines that could be used as a subroutine, or part of a larger program. REM stands for remark and is a note to yourself in the program. Use REM to label each part of a larger program so that when you look at your program you will see where each section begins and what it does. In Qbasic, an apostrophe can be used instead of REM.



This subroutine asks for two numbers as input and then adds them together.



ALT, F N

100 REM *** INPUT AND ADD

110 PRINT "WHAT ARE YOUR TWO NUMBERS"

120 INPUT N1, N2

130 PRINT N1," + ";N2;" = ";N1 + N2



Run the program.



This subroutine rounds a number down to the nearest whole number. The T in line 210 stands for total.

Add it to the program you just ran.



200 REM *** ROUNDING DOWN

210 LET T=INT(T)



These routines could work alone as programs, but they could also be incorporated as subroutines into a larger program with GOSUB and RETURN statements. GOSUB tells the computer to jump to a new line and follow instructions from there. RETURN tells the computer to return to the line in the main program immediately following the GOSUB line.



Programming Tip: Number subroutines as 100, 200 or 1000, 2000, or give them names, so they are easily identifiable in your program listing. Be sure to use an END statement at the end of the main program so that the computer won't drop down into its subroutines.

Now add these lines to the two subroutines. Run the program.



10 REM *** ADD AND ROUND PROGRAM ***

20 CLS:PRINT "I WILL ADD NUMBERS FOR YOU"

30 PRINT "AND ROUND DOWN THEIR TOTAL."

40 PRINT:GOSUB 100

50 LET T = N1 + N2

60 GOSUB 200

70 PRINT "YOUR TOTAL ROUNDED DOWN IS ";T

80 GOTO 40

90 END

140 RETURN

220 RETURN



Here is another program with a subroutine.



ALT, F N

10 CLS

20 PRINT "EXAMPLE OF GOSUB...RETURN"

30 PRINT "USING CELSIUS AND FAHRENHEIT:"

40 PRINT:PRINT "IF DEGREES CELSIUS IS 15,"

50 LET C = 15

60 GOSUB 1000

70 PRINT "IF DEGREES CELSIUS IS 25,"

80 LET C = 25

90 GOSUB 1000

100 PRINT "IF DEGREES CELSIUS IS 30,"

110 LET C = 30

120 GOSUB 1000

130 END

1000 PRINT "DEGREES FAHRENHEIT IS";

1010 PRINT 9 / 5 * C + 32"."

1020 PRINT

1030 RETURN





Activity 3



Music is one of the things that make computers so much fun. QBasic, makes it very simple to play music on the computer.



Type in this program as an example to play a popular Christmas carol.



10 PLAY "O3 L8 EE L4 E L8 EE L4 E L8 EGDC L2 E"

20 PLAY "L8 FFFFFE L16 EE L8 EDDE L4 DG"

30 PLAY "L8 EE L4 E L8 EE L4 E L8 EGDC L2 E"

40 PLAY "L8 FFFFFEE L16 EE L8 GGFD L2 C"



In line 10, O3 sets the octave of the music to the third octave. Octave 0 is the lowest octave, and 6 is the highest. L8 asks for eight notes (short.) This means that all the notes after that command be eighth notes until the length is changed again. The length of notes can be changed to any length from 1 (longest) to 64 (shortest). The letters, such as EE show the notes to play. In each octave, the notes are CDEFGAB, in order from lowest to highest.



There are some more commands you should be taught before you can write songs.



< or > = moves up or down one octave.

ML = plays the music legato (smooth)

MN = plays the music normally

MS = plays the music staccato (choppy music)



MF = plays the music in the foreground

MB = plays the music in the background



P1 = plays a pause with the length 1 (P2 would be pause length 2, etc.)

T1 = sets the tempo in quarter notes per minute (32 - 255)

# = turns the preceding note into a sharp

- = turns the preceding note into a flat

. = plays the preceding note 3/2 as long as specified



Write a song of your own that lasts more than 30 seconds.



Index

Flashing Text, 10

:, 27

$, 39

+, 12

-, 12

*, 12

/, 12

^, 17

<>, 27

ABS, 18

absolute value, 18

addition, 14

ALT F A, 42

ALT F O, A, 43

ALT, F N, 3, 4

animation, 53, 54

apostrophe, 68

arrays, 54

back arrow key, 1

Block IF, 28, 30, 31

Boxes, 48, 49

bug, 57

CASE, 30, 31

characters, 4

CIRCLE, 51

CLS, 3

colon, 10, 27

COLOR, 8, 10, 46-49

comma, 6

counting, 22, 29

Ctrl-Break, 4, 5

cumulative totals, 25

Debugging, 57

deleteing lines, 26

DIM, 54

Directory, 43

division, 14

ELSEIF, 30, 31

END, 3, 7, 23, 68

ENDIF, 28

exponents, 17

F5, 5

Filled Boxes, 49

Flowcharting, 65

FOR statement, 32-35

GET, 54

GOSUB, 68

GOTO, 4, 10, 32

Graphics, 45

IF ... THEN, 23, 27-31

INPUT, 24

INT, 17

INVERSE, 10

LEFT$, 40, 41

LEN, 39, 40

LET statement, 20

LINE, 45, 48, 49

Line numbers, 5

Loading, 42

LOCATE, 9

Logic Error, 57

loop, 4, 32

math operations, 12

Multiplication, 14

Music, 70

nested loop, 34, 35

Operations in parentheses, 14

PAINT, 51

pixels, 45, 51

PLAY, 70

points, 45

PRINT, 1, 2, 6

Program names, 42

PUT, 54

quotation mark, 2, 12

radius, 51

Random Numbers, 19, 36-38

RANDOMIZE TIMER, 19

remark, 68

reserved words, 20

RETURN, 68

RND, 19, 36-38

Robust Programs, 30

Saving, 42

SCREEN 12, 45, 47

SELECT CASE, 30, 31

semicolon, 6

Shift-F5., 1

Shift-F6, 1

spaces, 15

SQR, 18

square root, 18

STEP command, 32

string variable, 39

subroutine, 68

subscript, 40, 41

subtraction, 14

SYNTAX ERROR, 20, 57

TAB, 9

variable, 20, 21, 26

VGA graphics, 45

XOR, 54



Home