Compucolor.org – Virtual Media

Listing of file='TTRL5.BAS;01' on disk='vmedia/BASIC_language_1-5-sector.ccvf'

100 REM COPYRIGHT (C) BY COMPUCOLOR 1979

    BY KLH 10/28/79
110 CLEAR 1000:ADR= 256* PEEK (32941)+ PEEK (32940)
120 POKE ADR+ 41,5:POKE ADR+ 42,5
130 LN= PEEK (ADR+ 44):IF LN< > PEEK (ADR+ 45)OR LN= 0THEN 1000
140 ON LNGOTO 1002,1200,1300
1000 POKE ADR+ 44,1:POKE ADR+ 45,1
1002 PLOT 29,6,2,12,14,3,8,9:PRINT " LOOPS, SUBROUTINES, BRANCHING, AND FUNCTIONS 
1004 PRINT :PRINT TAB( 5)"BY NOW YOU ARE PROBABLY GETTING A FAIR UNDERSTANDING OF
1006 PRINT "HOW BASIC WORKS. YOUR PROGRAMMING SKILLS SHOULD ALSO BE GETTING
1008 PRINT " BETTER. NOW WE'RE GOING TO MOVE ON TO FOR - NEXT LOOPS, WHICH
1010 PRINT " WILL CAUSE A PORTION OF A PROGRAM TO BE RUN A DEFINED NUMBER
1012 PRINT TAB( 3)"OF TIMES. AN EXPLANATION WILL BE GIVEN OF GOSUB - RETURN
1014 PRINT " STATEMENTS, WHICH LETS A SUBROUTINE BE ACCESSED IN A PROGRAM.
1016 PRINT "THE ON-GOTO AND ON-GOSUB STATEMENTS WILL SHOW YOU HOW TO BRANCH
1018 PRINT "TO OTHER LINES AND SUBROUTINES DEPENDING ON A VARIABLE'S VALUE.
1020 PRINT "AND, YOU WILL ALSO LEARN FUNCTIONS WHICH PERFORM VARIOUS TASKS
1022 PRINT TAB( 14)"AND DO NUMERIC AND STRING OPERATIONS.":GOSUB 10030
1024 PLOT 3,5,7:PRINT "MANY TIMES IN A PROGRAM IT IS NECESSARY TO DO A CERTAIN
1026 PRINT "  ROUTINE, SUCH AS READ DATA, AND IT ISN'T FEASIBLE TO HAVE TEN
1028 PRINT "  READ STATEMENTS TO READ TEN DATA VALUES. A LOOP, WHICH CAUSES
1030 PRINT " A PORTION OF A PROGRAM TO RUN FOR A SET NUMBER OF TIMES, COULD
1032 PRINT TAB( 10)"BE MADE WITH AN ASSIGNMENT AND AN IF - THEN.
1034 PRINT "100 X= X+ 1
1036 PRINT "200 READ M
1038 PRINT "300 IF X< 10 THEN 100
1040 PRINT "400 DATA 1,2,3,4,5,6,7,8,9,10
1042 PRINT :PRINT TAB( 13)"BUT A FOR - NEXT LOOP IS MUCH SIMPLER:
1044 PRINT "100 FOR X= 1TO 10
1046 PRINT "200 READ M
1048 PRINT "300 NEXT X
1050 PRINT "400 DATA 1,2,3,4,5,6,7,8,9,10
1052 GOSUB 10030
1054 PLOT 3,2,10:PRINT "A LOOP IS A SET OF STATEMENTS THAT ARE REPEATED A DETERMINED
1056 PRINT "  NUMBER OF TIMES, EACH TIME BEING CHANGED UNTIL IT IS TOLD TO
1058 PRINT "STOP. FOR AND NEXT STATEMENTS ARE TWO DIFFERENT STATEMENTS THAT
1060 PRINT TAB( 11)"ARE USED TOGETHER TO DO LOOPS IN THIS FORM:
1062 PRINT TAB( 3)"FOR [VARIABLE] = [EXPSN 1] TO [EXPSN 2] ( STEP [EXPSN 3] )":PRINT ,,,"NEXT [VARIABLE]
1064 PRINT "  EXPSN IS A NUMERIC EXPRESSION, EITHER A NUMBER OR A VARIABLE,
1066 PRINT " THAT DEFINES HOW MANY TIMES THE LOOP SHOULD BE DONE. THE STEP
1068 PRINT " IS OPTIONAL, AND IF IT IS NOT INCLUDED IN THE FOR STATEMENT IS
1070 PRINT " ASSUMED TO BE 1. VARIABLE IS ANY NUMERIC VARIABLE, AND MUST BE
1072 PRINT "  THE SAME IN BOTH THE FOR AND NEXT STATEMENTS. NOTE: STEP CAN
1074 PRINT " BE POSITIVE OR NEGATIVE, AS LONG AS EXPSN 1 AND 2 ARE IN ORDER.
1076 GOSUB 10030
1078 PLOT 3,5,10:PRINT "LET'S SEE HOW THE FOLLOWING FOR - NEXT LOOP WORKS....
1080 PRINT "100 FOR X= 1TO 10STEP 2":PRINT "200 PRINT X,
1082 PRINT "300 NEXT X
1084 PRINT TAB( 20)"THE PROGRAM PRINTS THIS:":PRINT 1,3,5,7,9"
1086 PRINT " WHEN THE LOOP STARTS WITH THE FOR, X IS EQUAL TO 1. IT PRINTS
1088 PRINT " X, AND GOES ON UNTIL THE NEXT IS FOUND. THE COMPUTER GOES BACK
1090 PRINT "TO THE FOR AND THE VALUE OF X IS INCREASED BY THE AMOUNT OF THE
1092 PRINT TAB( 3)"STEP: SO NOW X EQUALS 3. THE LOOP WILL CONTINUE UNTIL X IS
1094 PRINT TAB( 6)"GREATER THAN 10, SO WHEN X EQUALS 11 THE LOOP ENDS.
1096 GOSUB 10030
1098 PLOT 3,2,6:PRINT "FOR - NEXT LOOPS CAN BE NESTED INSIDE ONE ANOTHER. THE ONLY
1100 PRINT TAB( 10)"PROVISION IS THAT THE LOOPS DO NOT CROSS.
1102 PRINT ,"   GOOD NESTING",,"    BAD NESTING
1104 PRINT ,"` 100 FOR X= 1TO 10","` 100 FOR X= 1TO 10
1106 PRINT ,"` 200 FOR Y= 1TO 10","` 200 FOR Y= 1TO 10
1108 PRINT ,"` 300 NEXT Y",,"` 300 NEXT X
1110 PRINT ,"` 400 NEXT X",,"` 400 NEXT Y
1112 PRINT TAB( 5)"AS YOU SEE IN THE GOOD EXAMPLE, THE Y LOOP (IN GREEN)
1114 PRINT "IS COMPLETELY EXECUTED BEFORE THE X LOOP (IN RED) IS CONTINUED.
1116 PRINT " IN THE BAD EXAMPLE, THE Y LOOP IS NEVER DONE UNTIL THE X LOOP
1118 PRINT "IS FINISHED, AND THEN A NF ERROR WILL OCCUR. THIS TELLS THE USER
1120 PLOT 28:PRINT TAB( 6)"IT FOUND A NEXT STATEMENT WITHOUT AN ACCOMPANYING FOR.
1122 PRINT "   A FOR - NEXT LOOP CAN BE EXITED BEFORE IT IS DONE, BUT IT
1124 PRINT "CANNOT BE ENTERED UNLESS IT HAS BEEN EXITED BEFORE, THUS HAVING
1126 PRINT TAB( 20)"THE LOOP ALREADY DEFINED.
1128 PRINT TAB( 13)"LINES 300 AND 400 COULD BE COMBINED TO:
1130 PRINT "300 NEXT Y,X
1132 PRINT ,"STILL KEEPING THE VARIABLES IN THE PROPER ORDER.
1134 GOSUB 10030
1136 PRINT "   THE TUTORIAL IS GOING TO STOP NOW TO LET YOU TRY SOME MORE
1138 PRINT ," PROGRAMMING. WHEN YOU READY TO GO ON HIT AUTO.
1140 POKE  ADR+ 44,2:POKE ADR+ 45,2:GOTO 1446
1200 REM
1202 PLOT 6,2,12,3,5,6:PRINT "THERE WILL COME A TIME WHEN A SECTION OF A PROGRAM MAY
1204 PRINT "  BE USED IN MANY DIFFERENT PLACES IN THE PROGRAM. IF THE PART
1206 PRINT " NEEDED IS VERY LARGE, IT WOULD TAKE UP TOO MUCH SPACE TO WRITE
1208 PRINT " THE ENTIRE SECTION EVERY TIME IT WAS NEEDED. THIS IS WHERE THE
1210 PRINT "  GOSUB AND RETURN STATEMENTS ARE USED. A GOSUB STATEMENT WILL
1212 PRINT " SEND THE COMPUTER TO A SUBROUTINE THAT PERFORMS A DESIRED TASK
1214 PRINT " THAT IS USED OFTEN AND CONTINUES TO EXECUTE THE PROGRAM THERE.
1216 PRINT " WHEN A RETURN STATEMENT IS FOUND, THE PROGRAM CONTINUES AT THE
1218 PRINT "   FIRST STATEMENT AFTER THE GOSUB. THEY ARE USED IN THE FORM:
1220 PRINT TAB( 22)"GOSUB [LINE NUMBER]":PRINT TAB( 29)"RETURN
1222 PRINT "   LINE NUMBER IS THE FIRST LINE OF THE SUBROUTINE. RETURN IS
1224 PRINT TAB( 14)"PLACED AT THE END OF THE SUBROUTINE.
1226 PRINT "   GOSUBS CAN ALSO BE NESTED, AND CAN EVEN CALL, OR GOSUB TO,
1228 PRINT " THEMSELVES. AS LONG AS EACH SUBROUTINE IS EXITED BY A RETURN,
1230 PRINT ," ALL THE SUBROUTINES WILL BE EXECUTED PROPERLY.
1232 GOSUB 10030
1234 PLOT 3,3,9:PRINT "YOU MAY ALSO WISH FOR A PROGRAM TO GO TO A LINE NUMBER OR
1236 PRINT "CALL A SUBROUTINE DEPENDING ON THE VALUE OF A CERTAIN VARIABLE.
1238 PRINT " THIS COULD BE DONE WITH IF - THEN STATEMENTS, BUT IF THERE ARE
1240 PRINT "MANY POSSIBILITIES TO CONSIDER, AN ON-GOTO OR ON-GOSUB STATEMENT
1242 PRINT TAB( 16)"IS MUCH EASIER AND SPACE-SAVING.
1244 PRINT "    ON-GOTO AND ON-GOSUB STATEMENTS WILL EITHER GO TO A LINE
1246 PRINT " NUMBER OR CALL A SUBROUTINE DEPENDING ON THE VALUE OF A GIVEN
1248 PRINT TAB( 10)"VARIABLE. BOTH STATEMENTS ARE IN THIS FORM:
1250 PRINT :PRINT TAB( 15)"ON [EXPRESSION] <  GOTO
  > [LIST]":PRINT ,,,,"  GOSUB
1252 GOSUB 10030
1254 PLOT 3,15,6:PRINT "ON [EXPRESSION] <  GOTO
  > [LIST]":PRINT ,,,,"  GOSUB
1256 PRINT TAB( 5)"EXPRESSION IS A NUMERIC VARIABLE, AND LIST ARE THE LINE
1258 PRINT " NUMBERS THAT ARE TO BE EXECUTED. THE EXPRESSION IS TURNED INTO
1260 PRINT "AN INTEGER, AND THE LINE NUMBER CORRESPONDING TO THE EXPRESSION
1262 PRINT " IS WHERE THE PROGRAM BRANCHES TO. IF THE EXPRESSION IS 0 OR IS
1264 PRINT "GREATER THAN THE NUMBER OF LINE NUMBERS IN THE LIST, THE PROGRAM
1266 PLOT 28:PRINT "  CONTINUES WITH THE NEXT STATEMENT. IF THE EXPRESSION IS LESS
1268 PRINT "  THAN 0 OR IS GREATER THAN 255, A CF ERROR (CALL FUNCTON) WILL
1270 PRINT TAB( 3)"OCCUR. AN ON-GOSUB WILL CALL THE SUBROUTINE, AND CONTINUE
1272 PRINT " WITH THE NEXT STATEMENT. AN EXAMPLE OF THE STATEMENT WOULD BE:
1274 PRINT "100 ON X(GOTO/GOSUB) 500,600,700,200,900
1276 :PRINT "200 REM X = 4 SO DON'T JUMP
1278 PRINT "  IF X = 1, THE PROGRAM JUMPS TO LINE 500; IF X = 2 IT GOES TO
1280 PRINT "   600; AND SO ON. NOTICE THAT WHEN X EQUALS 4 IT CONTINUES
1282 PRINT TAB( 5)"TO LINE 200, BUT LINE 200 MUST BE INCLUDED IN THE LIST
1284 PRINT " JUST AS ANY OTHER LINE WOULD, EVEN THOUGH IT IS THE NEXT LINE.
1286 GOSUB 10030
1288 PRINT "NEXT WE'LL BE DISCUSSING NUMERIC AND STRING FUNCTIONS, BUT FIRST
1290 PLOT 28:PRINT "   WE'LL STOP SO YOU CAN WRITE SOME PROGRAMS USING WHAT YOU'VE
1292 PRINT " LEARNED IN THIS LESSON. AGAIN, JUST HIT AUTO WHEN YOU'RE READY
1294 PRINT TAB( 26)"TO CONTINUE.":POKE ADR+ 44,3:POKE ADR+ 45,3:GOTO 1446
1300 REM
1302 PLOT 6,2,12,3,3,5:PRINT "THE COMPUCOLOR HAS MANY FUNCTIONS, BOTH NUMERIC AND STRING,
1304 PRINT " THAT IT CAN PERFORM BY GIVING THE NEEDED ARGUMENTS. HERE ARE A
1306 PRINT TAB( 5)"FEW THAT WILL BE HANDY DURING MUCH OF YOUR PROGRAMMING:
1308 PRINT "FUNCTION","  TYPE",,,"DESCRIPTION
1310 PRINT " FRE(X)",,"NUMERIC",,"GIVES THE NUMBER OF FREE BYTES*":PRINT ,,,,"  NOT IN USE
1312 PRINT " INT(X)",,"NUMERIC",,"GIVES THE GREATEST INTEGER LESS":PRINT ,,,,"  THAN OR EQUAL TO X
1314 PRINT " POS(X)",,"NUMERIC",,"GIVES THE CURSOR'S POSITION":PRINT ,,,,"  BETWEEN 0 AND 63
1316 PRINT " RND(X)",,"NUMERIC",,"GIVES A RANDOM NUMBER BETWEEN":PRINT ,,,,"  0 AND 1
1318 PRINT " SPC(X)",,"NUMERIC",,"PRINTS X SPACES (MUST BE":PRINT ,,,,"  USED WITH PRINT STATEMENT)
1320 PRINT " TAB(X)",,"NUMERIC",,"MOVES THE CURSOR TO COLUMN X":PRINT ,,,,"(MUST BE USED WITH PRINT)
1321 PRINT :PRINT TAB( 14)"* A BYTE IS A SINGLE MEMORY LOCATION.
1322 GOSUB 10030
1324 PLOT 3,0,3:PRINT "FUNCTION","  TYPE",,,"DESCRIPTION
1326 PRINT " ASC(X$)","STRING",,"GIVES ASCII VALUE FOR FIRST":PRINT ,,,,"  CHARACTER OF THE STRING
1328 PRINT " CHR$(X)","STRING",,"PRINTS THE ASCII CHARACTER WITH":PRINT ,,,,"  THE VALUE OF X (0=>X<=255)
1330 PRINT " FRE(X$)","STRING",,"RETURNS THE NUMBER OF FREE":PRINT ,,,,"  STRING BYTES
1332 PRINT " LEFT$(X$,I)","STRING",,"RETURNS I CHARACTERS FROM THE":PRINT ,,,,"  LEFT SIDE OF STRING X$
1334 PRINT " MID$(X$,N,I)","STRING",,"GIVES I CHARACTERS FROM STRING":PRINT ,,,,"  X$ STARTING AT THE NTH"
1336 PRINT ,,,,"  CHARACTER ( IF I IS OMITTED,":PRINT ,,,,"  ALL CHARACTERS FROM N TO THE":PRINT ,,,,"  END OF THE STRING ARE GIVEN.)
1338 PRINT " RIGHT$(X$,I)","STRING",,"GIVES I CHARACTERS FROM THE":PRINT ,,,,"  RIGHT SIDE OF STRING X$
1340 PRINT " STR$(X)","STRING",,"GIVES THE STRING REPRESENTED":PRINT ,,,,"  BY THE NUMERIC VARIABLE X
1342 PRINT " VAL(X$)","STRING",,"RETURNS THE NUMBER REPRESENTED":PRINT ,,,,"  BY THE STRING X$ (IF X$
1344 PRINT ,,,,"  IS NOT A NUMBER, 0 IS GIVEN.)
1346 PRINT :PRINT "   STRING FUNCTIONS MAY SEEM TERRIBLY CONFUSING NOW, BUT LATER
1348 PRINT "THEY WILL MAKE MORE SENSE. IN ANY CASE, THESE ARE THE FUNCTIONS.
1350 PLOT 28:GOSUB 10030
1352 PRINT :PRINT TAB( 3)"THIS CONCLUDES TUTORIAL FIVE. DO YOU WISH TO REVIEW THIS
1354 INPUT " LESSON (YES/NO)? ";ZZ$:GOSUB 10000:IF LEFT$ (ZZ$,1)= "Y"THEN 1002:REM 
1356 IF LEFT$ (ZZ$,1)< > "N"THEN PLOT 28,11:GOTO 1354
1358 PRINT :PRINT TAB( 3)"EXPERIMENT SOME WITH WHAT YOU HAVE LEARNED IN THIS LESSON.
1360 PRINT "   WHEN YOU ARE READY TO GO TO TUTORIAL SIX PUT IN THE SECOND
1362 PRINT "BASIC LANGUAGE DISK AND HIT THE AUTO KEY. IF YOU WISH TO REVIEW
1364 PRINT ,,"THIS DISK AGAIN, JUST HIT AUTO.
1442 POKE ADR+ 41,1:POKE ADR+ 42,1:POKE ADR+ 44,0:POKE ADR+ 45,0:PLOT 27,11
1444 PRINT :PRINT "HAVE FUN!"
1446 POKE 33434,0:POKE 33435,0:POKE 33436,0:END
10000 IF LEFT$ (ZZ$,1)= "M"THEN LOAD "MENU":RUN 1000
10010 IF PEEK (33278)< > 1THEN RETURN
10020 PLOT 1
10030 PRINT :INPUT "		    HIT RETURN TO CONTINUE: ";ZZ$:PLOT 12:GOTO 10000