home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / genie-commodore-file-library / Information / LEARN-BASIC < prev    next >
Encoding:
Text File  |  2019-04-13  |  15.6 KB  |  369 lines

  1. *********************************************************************
  2. This article is being presented through the *StarBoard* Journal of
  3. the FlagShip/StarShip, SIGS (Special Interest Groups) on the
  4. Delphi and GEnie telecommunications networks.  Permission is
  5. hereby granted to non-profit organizations only to reprint this
  6. article or pass it along electronically as long as proper credit
  7. is given to both the author and the *StarBoard* Journal.
  8. *********************************************************************
  9.  
  10.                      Learn BASIC by Debugging
  11.  
  12.                          by Tim Sickbert
  13.  
  14.                         (MIDNIT on DELPHI)
  15.  
  16.  
  17.      Learning BASIC can be intimidating. It doesn't matter how
  18. long you have had your computer, or how well you can process words,
  19. numbers, or names. Nor does it matter how many programs you have
  20. keyed in from magazine listings and "Learning BASIC" books. You
  21. may still not feel comfortable with BASIC.
  22.  
  23.      I got my computer about a year ago, primarily as a word
  24. processor, but also to learn a bit about programming. After six
  25. months, I had typed in about twenty or so programs from the
  26. manuals and from magazines, and I still felt that I didn't know a
  27. fool thing about how to write a program. Oh, I had written a few
  28. cute little things to make the screen change color and to print
  29. things out, but nothing that DID anything.
  30.  
  31.      The biggest problem I had was thinking of something to write
  32. a program for. All these cute little things did not add up to
  33. much, and I could see no reason to carry them further. It was
  34. really rather discouraging.
  35.  
  36.      Then, one day in a newsletter I found a short little listing
  37. for printing out mail labels. It was really pretty easy,
  38. and I never would have thought of doing the same thing myself.
  39. But I was bored, so I read the article that came with the
  40. listing, and typed it in. It turned out that I had learned
  41. something over the months. I had a good idea about how a program
  42. should be written, and I had a strong feeling about how the BASIC
  43. commands should be used. Everything I had learned added up to tell
  44. me that the program I was looking at needed a good little bit of
  45. work. INSPIRATION!!!
  46.  
  47.      I didn't need to come up with my own ideas for a program (I
  48. am not that creative) - I could take somebody else's idea, find
  49. its flaws (there is no such thing as a perfect piece of code),
  50. make it work better, and add my own bells and whistles.
  51.  
  52.      Every time I had come up with an idea of my own, I had
  53. figured, "Naw, that wouldn't be a REAL program...it would just be
  54. a few lines to make it look like I was programming."  Working on
  55. somebody else's program somehow made it more legitimate for me.
  56.  
  57.      The point of all this is that you can learn programming by
  58. taking somebody else's BASIC program, finding its flaws, and
  59. adding to the program to make it do more, and do it better. You
  60. probably won't want a program out of a magazine as they can get
  61. rather long and tedious. Probably the best thing to work on is a
  62. short routine that a moderate-level programmer would sit down and
  63. type in as a quickie, just to serve his immediate need. Many
  64. programmers will sit down and need to get something done pretty
  65. quick. Rather than write a fully developed program, they get down
  66. and dirty - they patch and kludge.
  67.  
  68.      If a programmer had a need for it, so does somebody else. The
  69. original version won't do because it was probably written pretty
  70. carelessly, and probably doesn't do much in the first place. But
  71. if you take that idea, clean it up, and expand it, you may be able
  72. to make a decent little utility out of it.
  73.  
  74.      I had a check bounce last fall, and was rather anxious to
  75. find out where I had made a mistake in balancing my checkbook. The
  76. batteries in my calculator were dead. I didn't have the patience
  77. to learn how to use a spreadsheet right then, and I REALLY didn't
  78. want to do it on paper. I typed in a quickie little program that
  79. looked a lot like this:
  80.  
  81.           10 b$=""
  82.           20 a$=""
  83.           30 get a$:ifa$=""then80
  84.           40 if val(a$)>0 and val(a$)<9 then
  85.              b$=b$+a$:printa$;:goto77
  86.           50 if a$="0" then b$=b$+a$:goto77
  87.           60 if a$="+" then b=val(b$):tt=tt+b:print,tt:goto75
  88.           70 if a$="-" then b=val(b$):tt=tt-b:print,tt:goto75
  89.           80 if a$="*" then b=val(b$):tt=tt*b:print,tt:goto75
  90.           90 if a$="/" then b=val(b$):tt=tt/b:print,tt:goto75
  91.  
  92.  
  93.      Pretty cheesy, eh? Well, it took me almost half an hour to
  94. write that and get it working the first time. I found the mistake
  95. in my checkbook, so I was happy. But it was not the kind of thing
  96. I would show off to my friends; it is rather lacking in features
  97. and elegance. I knew right away that it was a handy little thing,
  98. though, and that I would want to use it again sometime, the next
  99. time the batteries in my calculator died. So why not make it work
  100. a bit better? Add a few more features? Maybe make some kind of
  101. article out of it?
  102.  
  103.      Anyway, that little program is what I started out with. Look
  104. at it, type it in, run it. Not so hot, but it is a start. What
  105. problems does it have?
  106.  
  107.      To begin with, there is no cursor, no error checking, and no
  108. way to tell what is going on. But there IS an idea. I had started
  109. something. So, the first thing, of course, is to figure out what
  110. all would be nice in a progrm like this.
  111.  
  112.      Instructions are always nice - put in some REM statements to
  113. remind myself what I am doing; print messages to the screen so
  114. users, including me, know what the program was doing; and
  115. generally make the program prettier. In fact, when I expanded this
  116. program, the first thing I did was write the instructions and a
  117. blank program. In the instructions (VERY much edited since the
  118. original writing) I PRINTed everything I wanted the program to do.
  119. Then I entered a whole bunch of empty lines - line numbers with
  120. just a colon - with a REM statement at every even hundred line
  121. number (100, 200, ..., etc.) that told what routine I would put
  122. there. That gave me something of a structure, some idea of where I
  123. was going, and all I had to do was write the little routines. It
  124. helped me, anyway.
  125.  
  126.      I suggest that, if interested, you try to do the same kind of
  127. thing right now, before continuing with this article. Take the
  128. program above, or another little one that you have sitting around,
  129. figure out what it needs to make it useful, write the
  130. instructions, and set up the lines that you can fill in with
  131. routines. This can get you started programming. The rest of this
  132. article will be about the above program in its present state, as
  133. found in the listing at the end of this article. The program is
  134. still not fully developed, but is an example of a somewhat later
  135. stage.
  136.  
  137.      You should have the listing for the Simple Adding Machine
  138. before you as you look at this. It looks kind of convoluted. It
  139. doesn't really need to be, but there is a reason for it.
  140.  
  141.      As I was writing the program, it looked a whole lot
  142. different. Lines 1260 to the end of the program were at the very
  143. beginning; lines 1130 through 1240 followed right after that.
  144. Having the REMs and the setup part of the program at the beginning
  145. is how you usually see it. Well, the last thing I did was move
  146. these to the end because they take up space (and therefore time) if
  147. they are at the beginning of the program. This program spends most
  148. of its time GETting characters and going through loops and GOing
  149. TO. This can take a lot of time, especially on the C128 in SLOW
  150. mode. So I tried to put all the really busy parts of the program
  151. at the beginning.
  152.  
  153.      Outside of that, the REMarks pretty much document the
  154. structure of the rest of the program. The program flows pretty
  155. simply from the input routine, through the evaluation and printing
  156. of the character got, to the adding of the entries and totals to
  157. an array.
  158.  
  159.      The individual routines deserve some bit of attention as
  160. well. The first routine is a simple flashing cursor routine that
  161. does little more than show the user where an entry will go and
  162. wait for a key to be pressed. The actual routine is the four lines
  163. from 200 through 230, inclusive. The counter, 'ct', determines how
  164. fast the cursor blinks. The variable 'fl' determines whether the
  165. pseudo cursor (the space character, alternately normal and
  166. reversed), will be the background or the character color, to give
  167. it its flash.
  168.  
  169.      The next routine starts the evaluation of a character once it
  170. has been entered. Rather than use string literals, I decided to
  171. use the ASC() value of the CHR$(). I did that for two reasons. The
  172. first was to trap all the numeric characters which have an ASCII
  173. value ranging from 48 to 57. The test in the original program used
  174. the VAL() function, which was ok; but I had to use another line to
  175. test for the zero character becuase any non-numeric character
  176. returns a zero from the VAL() function. Note that in using the
  177. ASC() function, it is necessary on most Commodore machines to add
  178. a CHR$(0) to the string for it to work.
  179.  
  180.      A lot of lines in this and the next routine trap special
  181. characters such as line 20 (which allows you to use the minus sign
  182. for negative numbers), line 310 (which provides for the <DELETE>
  183. key), and other special characters.
  184.  
  185.      The next routine takes the characters that filtered through
  186. the above routine, traps the comma and the decimal point, and
  187. evaluates the function. Lines 480 and 500 are leftovers - the
  188. characters should have been trapped earlier.
  189.  
  190.      The storage feature is one of those not fully developed. Such
  191. a feature could come in handy when you are balancing a checkbook,
  192. and keep finding a mistake. What it needs is a print feature that
  193. would provide a hardcopy so you could compare it more easily to
  194. the actual entries in your checkbook. I leave that to you.
  195.  
  196.      Only two other routines deserve attention. The SEQ file print
  197. routine in lines 640-760 that provides for single and dual disk
  198. drives with any legal device number. The setup routine checks to
  199. see if the computer is a C64 or a C128. These are the two most
  200. Commodore computers right now, but lines 1150-1200 can be deleted
  201. or REMed out for other machines. The program is generic Commodore
  202. BASIC 2.0 and should run unaltered on any Commodore 65xx system,
  203. with the exception of those 6 lines.
  204.  
  205.      The program itself is no great shakes, but the idea of taking
  206. a simple program and debugging and expanding it can get you
  207. started in BASIC programming. Look for the little routines in
  208. newsletters, magazines, etc., and see what you can do with them.
  209. Stay away from the big, convoluted programs until you have had
  210. some practice, but then see what you can do with those as well.
  211.  
  212. [Copyright 1986, Timothy B. Sickbert. Permission granted to
  213. non-profit groups to reprint all or part of the article, or to
  214. pass it on via electronically coded files, provided credit is
  215. given the author. Please address questions, comments, or what have
  216. you, to Tim Sickbert, P.O. Box 1747, Champaign, IL 61820, or on
  217. Delphi to MIDNIT, on Quantum Link to The Paper.]
  218. *********************************************************************
  219.  
  220.                        SIMPLE ADDING MACHINE
  221.                          (program listing)
  222.  
  223. 100 goto920
  224. 110 rem "Notes and Credits at End of Program
  225. 120 :
  226. 130 rem "Start adding machine program here
  227. 140 :
  228. 150 rem "Simple flashing cursor input
  229. 160 :
  230. 170 tt=0
  231. 180 fl=1:b$=""
  232. 190 a$=""
  233. 200 get a$:if a$<>""then270
  234. 210 ct=ct+1: if ct<>10 then goto200
  235. 220 ct=0: fl=-fl:if fl=-1 then print " ";:goto200
  236. 230 print " ";:goto200
  237. 240 :
  238. 250 :rem "A key has been hit. Evaluate
  239. 260 :
  240. 270 a=asc(a$+chr$(0))
  241. 280 if a>47 and a<58 then b$=b$+a$:printa$;:goto190
  242. 290 if (len(b$))=0 and a$="-" then b$=a$:printa$;:goto190
  243. 300 if a>41 and a<48 then 410
  244. 310 if len(b$)>0 and a=20 then print
  245.     a$;:b$=left$(b$,(len(b$)-1)):goto190
  246. 320 rem above line deletes
  247. 330 if a = 94 then b=val(b$):tt=ttE}Ab:print"E}A";tab(8)tt:goto550
  248. 340 if a = 13 then a$="+":a=43:goto430
  249. 350 if a = 195 then tt=0: print"Clear"tab(8)tt:goto550: rem "
  250.     Shift C = clear
  251. 360 if a = 209 then 600: rem "Shifted Q
  252. 370 goto190
  253. 380 :
  254. 390 :  rem "Evaluate chr$( codes range 42-47
  255. 400 :
  256. 410 if a = 44 then 180 : rem comma
  257. 420 if a=46 then b$=b$+a$:printa$;:goto190: rem "DECIMAL POINT
  258. 430 print a$;: b=val(b$):print tab(8);:
  259. 440 on 48-a goto 510,500,490,480,470,460
  260. 450 rem "Multiply, Add, Comma, Subtract, Decimal, Divide
  261. 460 tt=tt*b:printtt:goto550
  262. 470 tt=tt+b:printtt:goto550
  263. 480 print "This line should never execute!!! COMMA":stop
  264. 490 tt=tt-b:printtt:goto550
  265. 500 print"This line should never execute!! DECIMAL POINT":stop
  266. 510 tt=tt/b:printtt:goto550
  267. 520 :
  268. 530 : rem "If storing, add to array
  269. 540 :
  270. 550 if aa=0 then goto 180
  271. 560 a1$(tc)=b$+a$:a2(tc)=tt:tc=tc+1:goto180
  272. 570 :
  273. 580 rem" Quit and close
  274. 590 :
  275. 600 input "Quit. Are you sure? y";rs$
  276. 610 if rs$<>"y"then 550
  277. 620 if aa=0 then end
  278. 630 input"Do you want to save the arrayed data in a sequential
  279.     disk file? n";rs$
  280. 640 if rs$<>"y" then end
  281. 650 input"Sequential file name ";fi$
  282. 660 input"Disk device number   8";de$:de=val(de$):if de<1 or de>30
  283.     then 660
  284. 670 input"Drive number   0";dr$:dr=val(dr$):ifdr<0ordr>1 then 670
  285. 680 print"Any key to continue with disk file print":
  286. 690 getrs$:ifrs$=""then690
  287. 700 close15:open15,de,15:gosub 840
  288. 710 open2,de,2,""+dr$+":"+fi$+",s,w":gosub840
  289. 720 for i=0 to tc:print#2,a1$(i),a2(i):next
  290. 730 gosub 840
  291. 740 print#2:close2
  292. 750 input#15,e1,e1$,et,es:print e1,e1$,et,es
  293. 760 close1
  294. 770 input"quit <q>, clear <c>, or continue <u>  c";rs$
  295. 780 if rs$="u" then goto 180
  296. 790 if rs$="c" then tt=0:tc=0:print"Clear"tab(8)tt:goto170
  297. 800 end
  298. 810 :
  299. 820 :  rem "Disk access error checking routine
  300. 830 :
  301. 840 input#15,e1,e1$,et,es
  302. 850 if e1<20 then return
  303. 860 printe1,e1$,et,es:close2:close15
  304. 870 print"Disk output aborted": goto 770
  305. 880 :
  306. 890 :
  307. 900 rem "Instructions, notes, etc
  308. 910 :
  309. 920 printchr$(147)chr$(14)
  310. 930 print" This program functions as a small
  311. 940 print" adding machine. It will properly
  312. 950 print" handle all mathematical operators, and
  313. 960 print" it provides an option for storing your
  314. 970 print" entries and running totals in an array
  315. 980 print" which may be saved as a sequential
  316. 990 print" file for later reference.
  317. 1000 print"   The program is still a little bit
  318. 1010 print" thin. Other features you can add
  319. 1020 print" might fill the array from a sequential
  320. 1030 print" or print a hardcopy of entries and
  321. 1040 print" totals to a printer.
  322. 1050 print"  This program will ignore keys other
  323. 1060 print" than the number keys, the numerical
  324. 1070 print" function keys, the <SHIFTED> 'c' (to
  325. 1080 print" clear the running total), and the
  326. 1090 print" <SHIFTED> 'q' (to quit).
  327. 1100 print"   Any Key to Continue
  328. 1110 geta$:ifa$=""then1110
  329. 1120 :
  330. 1130 rem "Setup
  331. 1140 :
  332. 1150 sy=0:if peek(65280) then sy=1:rem"sy=0 - C128; sy=1 - c64
  333. 1160 if sy=1thenpoke808,234:rem" disable <RUN/STOP>
  334. 1170 if sy=0 then input"  Do you want to use C128 FAST mode
  335.      y";rs$
  336. 1180 if rs$="y" then fast
  337. 1190 if sy=0 then key 1,"*":key3,"/":key5,"E}A"
  338. 1200 if sy=0 then print "F1=*, F3=/, F5=E}A"
  339. 1210 print"   Do you want to store your"
  340. 1220 input"  entries and totals in arrays   n";rs$
  341. 1230 if rs$="y" then aa=1:input" How many elements for
  342.      storage";a1%:dim a1$(a1%),a2(a1%):tc=1
  343. 1240 goto170
  344. 1250 :
  345. 1260 rem ***************************
  346. 1270 :
  347. 1280 rem "   Simple adding machine
  348. 1290 :
  349. 1300 rem "       Tim Sickbert
  350. 1310 rem "        June 1986
  351. 1320 :
  352. 1330 rem ***************************
  353. 1340 :
  354. 1350 rem" This program is written to accompany
  355. 1360 rem" an article on learning BASIC by
  356. 1370 rem" taking a simple BASIC program,
  357. 1380 rem" debugging it, trimming it down, and
  358. 1390 rem" adding features to it. You are
  359. 1400 rem" invited to do the same with this one.
  360. 1410 :
  361. 1420 rem"   If you have any questions, comments,
  362. 1430 rem"  or criticisms - or if you find any
  363. 1440 rem"  glaring examples of bad programming -
  364. 1450 rem"  please let me know.
  365. 1460 rem"    Send E-mail to:
  366. 1470 rem"     on Delphi - MIDNIT
  367. 1480 rem"     on QLink  - The Paper
  368.  
  369.