home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / gutenburg / guten96e / sgcpv22 / VARIABLE.22A < prev    next >
Text File  |  1996-06-05  |  99KB  |  1,795 lines

  1.     A letter can stand for a number. For example, x can stand for the number 47, as in this program:
  2. CLS
  3. x = 47
  4. PRINT x + 2
  5.     The second line says x stands for the number 47. In other words, x is a name for the number 47.
  6.     The bottom line says to print x + 2. Since x is 47, the x + 2 is 49; so the computer will print 49. That's the only number the computer will print; it will not print 47.
  7.  
  8.                                                                      Jargon
  9.     A letter that stands for a number is called a numeric variable. In that program, x is a numeric variable; it stands for the number 47. The value of x is 47. In that program, the statement ``x = 47'' is called an assignment statement, because it assigns 47 to x.
  10.  
  11.                                                                A variable is a box
  12.     When you run that program, here's what happens inside the computer.
  13.     The computer's random-access memory (RAM) consists of electronic boxes. When the computer encounters the line ``x = 47'', the computer puts 47 into box x, like this:
  14.        ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  15. box x  ³       47    ³
  16.        ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  17. Then when the computer encounters the line ``PRINT x + 2'', the computer prints what's in box x, plus 2; so the computer prints 49.
  18.  
  19.                                                                   Faster typing
  20.     Instead of typing ___ 
  21. x = 47
  22. you can type just this:
  23. x=47
  24. At the end of that line, when you press the ENTER key, the computer will automatically put spaces around the equal sign.
  25.     You've learned that the computer:
  26. automatically capitalizes computer words (such as CLS)
  27. automatically puts spaces around symbols (such as + and =)
  28. lets you type a question mark instead of the word PRINT
  29. So you can type just this:
  30. cls
  31. x=47
  32. ?x+2
  33. When you press ENTER at the end of each line, the computer will automatically convert your typing to this:
  34. CLS
  35. x = 47
  36. PRINT x + 2
  37.                                                                                                                                                                                                                               More examples
  38.     Here's another example:
  39. CLS
  40. y = 38
  41. PRINT y - 2
  42.     The second line says y is a numeric variable that stands for the number 38.
  43.     The bottom line says to print y - 2. Since y is 38, the y - 2 is 36; so the computer will print 36.
  44.     Example:
  45. CLS
  46. b = 8
  47. PRINT b * 3
  48. The second line says b is 8. The bottom line says to print b * 3, which is 8 * 3, which is 24; so the computer will print 24.
  49.     One variable can define another:
  50. CLS
  51. n = 6
  52. d = n + 1
  53. PRINT n * d
  54. The second line says n is 6. The next line says d is n + 1, which is 6 + 1, which is 7; so d is 7. The bottom line says to print n * d, which is 6 * 7, which is 42; so the computer will print 42.
  55.  
  56.                                                                                                                                                                                                                             Changing a value
  57.     A value can change:
  58. CLS
  59. k = 4
  60. k = 9
  61. PRINT k * 2
  62.     The second line says k's value is 4. The next line changes k's value to 9, so the bottom line prints 18.
  63.     When you run that program, here's what happens inside the computer's RAM. The second line (k = 4) makes the computer put 4 into box k:
  64.        ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  65. box k  ³        4    ³
  66.        ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  67. The next line (k = 9) puts 9 into box k. The 9 replaces the 4:
  68.        ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  69. box k  ³        9    ³
  70.        ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  71. That's why the bottom line (PRINT k * 2) prints 18.
  72.  
  73.                                                                                                                                                                                                                                  Hassles
  74.     When writing an equation (such as x = 47), here's what you must put before the equal sign: the name of just one box (such as x). So before the equal sign, put one variable:
  75. Allowed                Not allowed                Not allowed
  76. d = n + 1                d - n = 1                        1 = d - n
  77.                                                        
  78. one variable            two variables                    not a variable
  79.     The variable on the left side of the equation is the only one that changes. For example, the statement d = n + 1 changes the value of d but not n. The statement b = c changes the value of b but not c:
  80. CLS
  81. b = 1
  82. c = 7
  83. b = c
  84. PRINT b + c
  85. The fourth line changes b, to make it equal c; so b becomes 7. Since both b and c are now 7, the bottom line prints 14.
  86.     ``b = c'' versus ``c = b'' Saying ``b = c'' has a different effect from ``c = b''. That's because ``b = c'' changes the value of b (but not c); saying ``c = b'' changes the value of c (but not b).
  87.     Compare these programs:
  88. CLS                    CLS
  89. b = 1                    b = 1
  90. c = 7                    c = 7
  91. b = c                    c = b
  92. PRINT b + c            PRINT b + c
  93.     In the left program (which you saw before), the fourth line changes b to 7, so both b and c are 7. The bottom line prints 14.
  94.     In the right program, the fourth line changes c to 1, so both b and c are 1. The bottom line prints 2.
  95.     While you run those programs, here's what happens inside the computer's RAM. For both programs, the second and third lines do this:
  96.        ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  97. box b  ³        1    ³
  98.        ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  99.        ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  100. box c  ³        7    ³
  101.        ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  102. In the left program, the fourth line makes the number in box b become 7 (so both boxes contain 7, and the bottom line prints 14). In the right program, the fourth line makes the number in box c become 1 (so both boxes contain 1, and the bottom line prints 2).                                                                                                                                                                                                   When to use variables
  103.     Here's a practical example of when to use variables.
  104.     Suppose you're selling something that costs $1297.43, and you want to do these calculations:
  105. multiply $1297.43 by 2
  106. multiply $1297.43 by .05
  107. add      $1297.43 to $483.19
  108. divide   $1297.43 by 37
  109. subtract $1297.43 from $8598.61
  110. multiply $1297.43 by 28.7
  111.     To do those six calculations, you could run this program:
  112. CLS
  113. PRINT 1297.43 * 2; 1297.43 * .05; 1297.43 + 483.19; 1297.43 / 37
  114. PRINT 8598.61-1297.43; 1297.43 * 28.7
  115. But that program's silly, since it contains the number 1297.43 six times. This program's briefer, because it uses a variable:
  116. CLS
  117. c = 1297.43
  118. PRINT c * 2; c * .05; c + 483.19; c / 37; 8598.61 - c; c * 28.7
  119.     So whenever you need to use a number several times, turn the number into a variable, which will make your program briefer.
  120.  
  121.                                                                                                                                                                                                     String variables
  122.     A string is any collection of characters, such as ``I love you''. Each string must be in quotation marks.
  123.     A letter can stand for a string ___ if you put a dollar sign after the letter, like this:
  124. CLS
  125. g$ = "down"
  126. PRINT g$
  127.     The second line says g$ stands for the string ``down''. The bottom line prints:
  128. down
  129.     In that program, g$ is a variable. Since it stands for a string, it's called a string variable.
  130.     Every string variable must end with a dollar sign. The dollar sign is supposed to remind you of a fancy S, which stands for String. The second line is pronounced, ``g String is down''.
  131.     If you're paranoid, you'll love this program:
  132. CLS
  133. t$ = "They're laughing at you!"
  134. PRINT t$
  135. PRINT t$
  136. PRINT t$
  137. The second line says t$ stands for the string ``They're laughing at you!''. The later lines make the computer print:
  138. They're laughing at you!
  139. They're laughing at you!
  140. They're laughing at you!
  141.                                      Spaces between strings
  142.     Examine this program:
  143. CLS
  144. s$ = "sin"
  145. k$ = "king"
  146. PRINT s$; k$
  147. The bottom line says to print ``sin'' and then ``king'', so the computer will print:
  148. sinking
  149.     Let's make the computer leave a space between ``sin'' and ``king'', so the computer prints:
  150. sin king
  151. To make the computer leave that space, choose one of these methods. . . . 
  152.     Method 1. Instead of saying ___ 
  153. s$ = "sin"
  154. make s$ include a space:
  155. s$ = "sin "
  156.     Method 2. Instead of saying ___ 
  157. k$ = "king"
  158. make k$ include a space:
  159. k$ = " king"
  160.     Method 3. Instead of saying ___ 
  161. PRINT s$; k$
  162. say to print s$, then a space, then k$:
  163. PRINT s$; " "; k$
  164. Since the computer will automatically insert the semicolons, you can type just this ___ 
  165. PRINT s$ " " k$
  166. or even type just this:
  167. PRINT s$" "k$
  168. or even type just this:
  169. ?s$" "k$
  170. When you press the ENTER key at the end of that line, the computer will automatically convert it to:
  171. PRINT s$; " "; k$
  172.  
  173.                                          Nursery rhymes
  174.     The computer can recite nursery rhymes:
  175. CLS
  176. p$ = "Peas porridge "
  177. PRINT p$; "hot!"
  178. PRINT p$; "cold!"
  179. PRINT p$; "in the pot,"
  180. PRINT "Nine days old!"
  181.     The second line says p$ stands for ``Peas porridge ''. The later lines make the computer print:
  182. Peas porridge hot!
  183. Peas porridge cold!
  184. Peas porridge in the pot,
  185. Nine days old!
  186.     This program prints a fancier rhyme:
  187. CLS
  188. h$ = "Hickory, dickory, dock! "
  189. m$ = "THE MOUSE (squeak! squeak!) "
  190. c$ = "THE CLOCK (tick! tock!) " PRINT h$
  191. PRINT m$; "ran up "; c$
  192. PRINT c$; "struck one"
  193. PRINT m$; "ran down"
  194. PRINT h$
  195. Lines 2-4 define h$, m$, and c$. The later lines make the computer print:
  196. Hickory, dickory, dock!
  197. THE MOUSE (squeak! squeak!) ran up THE ClOCK (tick! tock!)
  198. THE CLOCK (tick! tock!) struck one
  199. THE MOUSE (squeak! squeak!) ran down
  200. Hickory, dickory, dock!
  201.  
  202.                                                                                                                                                                                                    Undefined variables
  203.     If you don't define a numeric variable, the computer assumes it's zero:
  204. CLS
  205. PRINT r
  206. Since r hasn't been defined, the bottom line prints zero.
  207.     The computer doesn't look ahead:
  208. CLS
  209. PRINT j
  210. j = 5
  211. When the computer encounters the second line (PRINT j), it doesn't look ahead to find out what j is. As of the second line, j is still undefined, so the computer prints zero.
  212.     If you don't define a string variable, the computer assumes it's blank:
  213. CLS
  214. PRINT f$
  215. Since f$ hasn't been defined, the ``PRINT f$'' makes the computer print a line that says nothing; the line the computer prints is blank.
  216.  
  217.                                                                                                                                                                                                    Long variable names
  218.     A numeric variable's name can be a letter (such as x) or a longer combination of characters, such as:
  219. profit.in.1996.before.November.promotion
  220.     For example, you can type:
  221. CLS
  222. profit.in.1996.before.November.promotion = 3497.18
  223. profit.in.1996 = profit.in.1996.before.November.promotion + 6214.27
  224. PRINT profit.in.1996
  225. The computer will print:
  226.  9711.45
  227.     The variable's name can be quite long: up to 40 characters!
  228.     The first character in the name must be a letter. The remaining characters can be letters, digits, or periods.
  229.     The name must not be a word that has a special meaning to the computer. For example, the name cannot be ``print''.
  230.     If the variable stands for a string, the name can have up to 40 characters, followed by a dollar sign, making a total of 41 characters, like this:
  231. my.job.in.1996.before.November.promotion$
  232.     Beginners are usually too lazy to type long variable names, so beginners use variable names that are short. But when you become a pro and write a long, fancy program containing hundreds of lines and hundreds of variables, you should use long variable names to help you remember each variable's purpose.
  233.     In this book, I'll use short variable names in short programs (so you can type those programs quickly), and long variable names in long programs (so you can keep track of which variable is which).
  234.     Programmers employed at Microsoft capitalize the first letter of each word and omit the periods. So instead of writing:
  235. my.job.in.1996.before.November.promotion$
  236. those programmers write:
  237. MyJobIn1996BeforeNovemberPromotion$
  238. That's harder to read; but since Microsoft is headed by Bill Gates, who's the richest person in America, he can do whatever he pleases!     Humans ask questions; so to turn the computer into a human, you must make it ask questions too. To make the computer ask a question, use the word INPUT.
  239.     This program makes the computer ask for your name:
  240. CLS
  241. INPUT "What is your name"; n$
  242. PRINT "I adore anyone whose name is "; n$
  243.     When the computer sees that INPUT line, the computer asks ``What is your name?'' and then waits for you to answer the question. Your answer will be called n$. For example, if you answer Maria, then n$ is Maria. The bottom line makes the computer print:
  244. I adore anyone whose name is Maria
  245.     When you run that program, here's the whole conversation that occurs between the computer and you; I've underlined the part typed by you. . . . 
  246. The computer asks for your name:    What is your name? Maria
  247. The computer praises your name:    I adore anyone whose name is Maria
  248.     Try that example. Be careful! When you type the INPUT line, make sure you type the two quotation marks and the semicolon. You don't have to type a question mark: when the computer runs your program, it will automatically put a question mark at the end of the question.
  249.     Just for fun, run that program again and pretend you're somebody else. . . . 
  250. The computer asks for your name:    What is your name? Bud
  251. The computer praises your name:    I adore anyone whose name is Bud
  252.     When the computer asks for your name, if you say something weird, the computer will give you a weird reply. . . . 
  253. Computer asks your name:    What is your name? none of your business!!!
  254. The computer replies:        I adore anyone whose name is none of your business!!!
  255.  
  256.                                                                                        College admissions
  257.     This program prints a letter, admitting you to the college of your choice:
  258. CLS
  259. INPUT "What college would you like to enter"; c$
  260. PRINT "Congratulations!"
  261. PRINT "You have just been admitted to "; c$
  262. PRINT "because it fits your personality."
  263. PRINT "I hope you go to "; c$; "."
  264. PRINT "           Respectfully yours,"
  265. PRINT "           The Dean of Admissions"
  266.     When the computer sees the INPUT line, the computer asks ``What college would youl like to enter?'' and waits for you to answer. Your answer will be called c$. If you'd like to be admitted to Harvard, you'll be pleased. . . . 
  267. The computer asks you:    What college would you like to enter? Harvard
  268. The computer admits you:    Congratulations!
  269.                             You have just been admitted to Harvard
  270.                             because it fits your personality.
  271.                             I hope you go to Harvard.
  272.                                        Respectfully yours,
  273.                                        The Dean of Admissions
  274.     You can choose any college you wish:
  275. The computer asks you:    What college would you like to enter? Hell
  276. The computer admits you:    Congratulations!
  277.                             You have just been admitted to Hell
  278.                             because it fits your personality.
  279.                             I hope you go to Hell.
  280.                                        Respectfully yours,
  281.                                        The Dean of Admissions
  282.     That program consists of three parts:
  283.     1. The computer begins by asking you a question (``What college would you like to enter?''). The computer's question is called the prompt, because it prompts you to answer.
  284.     2. Your answer (the college's name) is called your input, because it's information that you're putting into the computer.
  285.     3. The computer's reply (the admission letter) is called the computer's output, because it's the final answer that the computer puts out.
  286.  
  287.                                                                                                                                                                                                                                                    INPUT versus PRINT
  288.     The word INPUT is the opposite of the word PRINT.
  289.     The word PRINT makes the computer print information out. The word INPUT makes the computer take information in.
  290.     What the computer prints out is called the output. What the computer takes in is called your input.
  291.     Input and Output are collectively called I/O, so the INPUT and PRINT statements are called I/O statements.                                                                                         Once upon a time
  292.     Let's make the computer write a story, by filling in the blanks:
  293. Once upon a time, there was a youngster named _____________
  294.                                                 your name
  295.  
  296. who had a friend named _________________.
  297.                          friend's name
  298.  
  299. _____________ wanted to ________________________ _________________,
  300.   your name               verb (such as "pat")     friend's name
  301.  
  302. but _________________ didn't want to ________________________ _____________!
  303.       friend's name                    verb (such as "pat")     your name
  304.  
  305. Will _____________ _______________________ _________________?
  306.        your name     verb (such as "pat")    friend's name
  307.  
  308. Will _________________ ________________________ _____________?
  309.        friend's name     verb (such as "pat")     your name
  310.  
  311. To find out, come back and see the next exciting episode
  312.  
  313.  
  314. of _____________ and _________________!
  315.      your name         friend's name
  316.     To write the story, the computer must ask for your name, your friend's name, and a verb. To make the computer ask, your program must say INPUT:
  317. CLS
  318. INPUT "What is your name"; y$
  319. INPUT "What's your friend's name"; f$
  320. INPUT "In 1 word, say something you can do to your friend"; v$
  321. Then make the computer print the story:
  322. PRINT "Here's my story...."
  323. PRINT "Once upon a time, there was a youngster named "; y$
  324. PRINT "who had a friend named "; f$; "."
  325. PRINT y$; " wanted to "; v$; " "; f$; ","
  326. PRINT "but "; f$; " didn't want to "; v$; " "; y$; "!"
  327. PRINT "Will "; y$; " "; v$; " "; f$; "?"
  328. PRINT "Will "; f$; " "; v$; " "; y$; "?"
  329. PRINT "To find out, come back and see the next exciting episode"
  330. PRINT "of "; y$; " and "; f$; "!"
  331.     Here's a sample run:
  332. What's your name? Dracula
  333. What's your friend's name? Madonna
  334. In 1 word, say something you can do to your friend? bite
  335. Here's my story....
  336. Once upon a time, there was a youngster named Dracula
  337. who had a friend named Madonna.
  338. Dracula wanted to bite Madonna,
  339. but Madonna didn't want to bite Dracula!
  340. Will Dracula bite Madonna?
  341. Will Madonna bite Dracula?
  342. To find out, come back and see the next exciting episode
  343. of Dracula and Madonna!
  344.     Here's another run:
  345. What's your name? Superman
  346. What's your friend's name? King Kong
  347. In 1 word, say something you can do to your friend? tickle
  348. Here's my story....
  349. Once upon a time, there was a youngster named Superman
  350. Who had a friend named King Kong.
  351. Superman wanted to tickle King Kong,
  352. but King Kong didn't want to tickle Superman!
  353. Will Superman tickle King Kong?
  354. Will King Kong tickle Superman?
  355. To find out, come back and see the next exciting episode
  356. of Superman and King Kong!
  357.     Try it: put in your own name, the name of your friend, and something you'd like to do to your friend.                                                                                              Contest
  358.     The following program prints a certificate saying you won a contest. Since the program contains many variables, it uses long variable names to help you remember which variable is which:
  359. CLS
  360. INPUT "What's your name"; you$
  361. INPUT "What's your friend's name"; friend$
  362. INPUT "What's the name of another friend"; friend2$
  363. INPUT "Name a color"; color$
  364. INPUT "Name a place"; place$
  365. INPUT "Name a food"; food$
  366. INPUT "Name an object"; object$
  367. INPUT "Name a part of the body"; part$
  368. INPUT "Name a style of cooking (such as baked or fried)"; style$
  369. PRINT
  370. PRINT "Congratulations, "; you$; "!"
  371. PRINT "You've won the beauty contest, because of your gorgeous "; part$; "."
  372. PRINT "Your prize is a "; color$; " "; object$
  373. PRINT "plus a trip to "; place$; " with your friend "; friend$
  374. PRINT "plus--and this is the best part of all--"
  375. PRINT "dinner for the two of you at "; friend2$; "'s new restaurant,"
  376. PRINT "where "; friend2$; " will give you ";
  377. PRINT "all the "; style$; " "; food$; " you can eat."
  378. PRINT "Congratulations, "; you$; ", today's your lucky day!"
  379. PRINT "Now everyone wants to kiss your award-winning "; part$; "."
  380.     Here's a sample run:
  381. What's your name? Long John Silver
  382. What's your friend's name? the parrot
  383. What's the name of another friend? Jim
  384. Name a color? gold
  385. Name a place? Treasure Island
  386. Name a food? rum-soaked coconuts
  387. Name an object? chest of jewels
  388. Name a part of the body? missing leg
  389. Name a style of cooking (such as baked or fried)? barbecued
  390.  
  391. Congratulations, Long John Silver!
  392. You've won the beauty contest, because of your gorgeous missing leg.
  393. Your prize is a gold chest of jewels
  394. plus a trip to Treasure Island with your friend the parrot
  395. plus--and this is the best part of all--
  396. dinner for the two of you at Jim's new restaurant,
  397. where Jim will give you all the barbecued rum-soaked coconuts you can eat.
  398. Congratulations, Long John Silver, today's your lucky day!
  399. Now everyone wants to kiss your award-winning missing leg.
  400.     This run describes the contest that brought Ronald Reagan to the White House:
  401. What's your name? Ronnie Reagan
  402. What's your friend's name? Nancy
  403. What's the name of another friend? Alice
  404. Name a color? red-white-and-blue
  405. Name a place? the White House
  406. Name a food? jelly beans
  407. Name an object? cowboy hat
  408. Name a part of the body? cheeks
  409. Name a style of cooking (such as baked or fried)? steamed
  410.  
  411. Congratulations, Ronnie Reagan!
  412. You've won the beauty contest, because of your gorgeous cheeks.
  413. Your prize is a red-white-and-blue cowboy hat
  414. plus a trip to the White House with your friend Nancy
  415. plus--and this is the best part of all--
  416. dinner for the two of you at Alice's new restaurant,
  417. where Alice will give you all the steamed jelly beans you can eat.
  418. Congratulations, Ronnie Reagan, today's your lucky day!
  419. Now everyone wants to kiss your award-winning cheeks.
  420.                                                                                               Bills
  421.     If you're a nasty bill collector, you'll love this program:
  422. CLS
  423. INPUT "What is the customer's first name"; first.name$
  424. INPUT "What is the customer's last name"; last.name$
  425. INPUT "What is the customer's street address"; street.address$
  426. INPUT "What city"; city$
  427. INPUT "What state"; state$
  428. INPUT "What ZIP code"; zip.code$
  429. PRINT
  430. PRINT first.name$; " "; last.name$
  431. PRINT street.address$
  432. PRINT city$; " "; state$; " "; zip.code$
  433. PRINT
  434. PRINT "Dear "; first.name$; ","
  435. PRINT "   You still haven't paid the bill."
  436. PRINT "If you don't pay it soon, "; first.name$; ","
  437. PRINT "I'll come visit you in "; city$
  438. PRINT "and personally shoot you."
  439. PRINT "            Yours truly,"
  440. PRINT "            Sure-as-shootin'"
  441. PRINT "            Your crazy creditor"
  442.     Can you figure out what that program does?
  443.  
  444.                                                                                           Numeric input
  445.     This program makes the computer predict your future:
  446. CLS
  447. PRINT "I predict what'll happen to you in the year 2000!"
  448. INPUT "In what year were you born"; y
  449. PRINT "In the year 2000, you'll turn"; 2000 - y; "years old."
  450.     Here's a sample run:
  451. I predict what'll happen to you in the year 2000!
  452. In what year were you born? 1962
  453. In the year 2000, you'll turn 38 years old.
  454.     Suppose you're selling tickets to a play. Each ticket costs $2.79. (You decided $2.79 would be a nifty price, because the cast has 279 people.) This program finds the price of multiple tickets:
  455. CLS
  456. INPUT "How many tickets"; t
  457. PRINT "The total price is $"; t * 2.79
  458.     This program tells you how much the ``energy crisis'' costs you, when you drive your car:
  459. CLS
  460. INPUT "How many miles do you want to drive"; m
  461. INPUT "How many pennies does a gallon of gas cost"; p
  462. INPUT "How many miles-per-gallon does your car get"; r
  463. PRINT "The gas for your trip will cost you $"; m * p / (r * 100)
  464. Here's a sample run:
  465. How many miles do you want to drive? 400
  466. How many pennies does a gallon of gas cost? 95.9
  467. How many miles-per-gallon does your car get? 31
  468. The gas for your trip will cost you $ 12.37419
  469.                                                                                            Conversion
  470.     This program converts feet to inches:
  471. CLS
  472. INPUT "How many feet"; f
  473. PRINT f; "feet ="; f * 12; "inches"
  474.     Here's a sample run:
  475. How many feet? 3
  476.  3 feet = 36 inches
  477.     Trying to convert to the metric system? This program converts inches to centimeters:
  478. CLS
  479. INPUT "How many inches"; i
  480. PRINT i; "inches ="; i * 2.54; "centimeters"
  481.     Nice day today, isn't it? This program converts the temperature from Celsius to Fahrenheit:
  482. CLS
  483. INPUT "How many degrees Celsius"; c
  484. PRINT c; "degrees Celsius ="; c * 1.8 + 32; "degrees Fahrenheit"
  485. Here's a sample run:
  486. How many degrees Celsius? 20
  487.  20 degrees Celsius = 68 degrees Fahrenheit
  488.     See, you can write the Guide yourself! Just hunt through any old math or science book, find any old formula (such as f = c * 1.8 + 32), and turn it into a program.     Let's write a program so that if the human is less than 18 years old, the computer will say:
  489. You are still a minor.
  490.     Here's the program:
  491. CLS
  492. INPUT "How old are you"; age
  493. IF age < 18 THEN PRINT "You are still a minor"
  494. Line 2 makes the computer ask ``How old are you'' and wait for the human to type an age. Since the symbol for ``less than'' is ``<'', the bottom line says: if the age is less than 18, then print ``You are still a minor''.
  495.     Go ahead! Run that program! The computer begins the conversation by asking:
  496. How old are you?
  497. Try saying you're 12 years old, by typing a 12, so the screen looks like this:
  498. How old are you? 12
  499. When you finish typing the 12 and press the ENTER key at the end of it, the computer will reply:
  500. You are still a minor
  501.     Try running that program again, but this time try saying you're 50 years old instead of 12, so the screen looks like this:
  502. How old are you? 50
  503. When you finish typing the 50 and press the ENTER key at the end of it, the computer will not say ``You are still a minor''. Instead, the computer will say nothing ___ since we didn't teach the computer how to respond to adults yet!
  504.     In that program, the most important line says:
  505. IF age < 18 THEN PRINT "You are still a minor"
  506. That line contains the words IF and THEN. Whenever you say IF, you must also say THEN. Do not put a comma before THEN. What comes between IF and THEN is called the condition; in that example, the condition is ``age < 18''. If the condition is true (if age is really less than 18), the computer does the action, which comes after the word THEN and is:
  507. PRINT "You are still a minor"
  508.  
  509.                                                                                               ELSE
  510.     Let's teach the computer how to respond to adults. Here's how to program the computer so that if the age is less than 18, the computer will say ``You are still a minor'', but if the age is not less than 18 the computer will say ``You are an adult'' instead:
  511. CLS
  512. INPUT "How old are you"; age
  513. IF age < 18 THEN PRINT "You are still a minor" ELSE PRINT "You are an adult"
  514.     In programs, the word ``ELSE'' means ``otherwise''. That program's bottom line means: if the age is less than 18, then print ``You are still a minor''; otherwise (if the age is not less than 18), print ``You are an adult''. So the computer will print ``You are still a minor'' or else print ``You are an adult'', depending on whether the age is less than 18.
  515.     Try running that program! If you say you're 50 years old, so the screen looks like this ___ 
  516. How old are you? 50
  517. the computer will reply by saying:
  518. You are an adult
  519.                                                                                           Multi-line IF
  520.     If the age is less than 18, here's how to make the computer print ``You are still a minor'' and also print ``Ah, the joys of youth'':
  521. IF age < 18 THEN PRINT "You are still a minor": PRINT "Ah, the joys of youth"
  522.     Here's a more sophisticated way to say the same thing:
  523. IF age < 18 THEN
  524.         PRINT "You are still a minor"
  525.         PRINT "Ah, the joys of youth"
  526. END IF
  527. That sophisticated way (in which you type 4 short lines instead of a single long line) is called a multi-line IF (or a block IF).
  528.     In a multi-line IF:
  529. The top line must say IF and THEN (with nothing after THEN).
  530. The middle lines should be indented; they're called the BLOCK and typically say PRINT.
  531. The bottom line must say END IF.
  532.     In the middle of a multi-line IF, you can say ELSE:
  533. IF age < 18 THEN
  534.         PRINT "You are still a minor"
  535.         PRINT "Ah, the joys of youth"
  536. ELSE
  537.         PRINT "You are an adult"
  538.         PRINT "We can have adult fun"
  539. END IF
  540. That means: if the age is less than 18, then print ``You are still a minor'' and ``Ah, the joys of youth''; otherwise (if age not under 18) print ``You are an adult'' and ``We can have adult fun''.
  541.  
  542.                                                                                              ELSEIF
  543.     Let's say this:
  544. If age is under 18, print ``You're a minor''.
  545. If age is NOT under 18 but is under 100, print ``You're a typical adult''.
  546. If age is NOT under 100 but is under 125, print ``You're a centenarian''.
  547. If age is NOT under 125, print ``You're a liar''.
  548.     Here's how:
  549. IF age < 18 THEN
  550.         PRINT "You're a minor"
  551. ELSEIF age < 100 THEN
  552.         PRINT "You're a typical adult"
  553. ELSEIF age < 125 THEN
  554.         PRINT "You're a centenarian"
  555. ELSE
  556.         PRINT "You're a liar"
  557. END IF
  558.     One word In QBASIC, ``ELSEIF'' is one word. Type ``ELSEIF'', not ``ELSE IF''. If you accidentally type ``ELSE IF'', the computer will gripe.     Let's turn your computer into a therapist!
  559.     To make the computer ask the patient, ``How are you?'', begin the program like this:
  560. CLS
  561. INPUT "How are you"; a$
  562.     Make the computer continue the conversation by responding as follows:
  563. If the patient says ``fine'', print ``That's good!''
  564. If the patient says ``lousy'' instead, print ``Too bad!''
  565. If the patient says anything else instead, print ``I feel the same way!''
  566. To accomplish all that, you can use a multi-line IF:
  567. IF a$ = "fine" THEN
  568.         PRINT "That's good!"
  569. ELSEIF a$ = "lousy" THEN
  570.         PRINT "Too bad!"
  571. ELSE
  572.         PRINT "I feel the same way!"
  573. END IF
  574.     Instead of typing that multi-line IF, you can type this SELECT statement instead, which is briefer and simpler:
  575. SELECT CASE a$
  576.         CASE "fine"
  577.                 PRINT "That's good!"
  578.         CASE "lousy"
  579.                 PRINT "Too bad!"
  580.         CASE ELSE
  581.                 PRINT "I feel the same way!"
  582. END SELECT
  583. Like a multi-line IF, a SELECT statement consumes several lines. The top line of that SELECT statement tells the computer to analyze a$ and SELECT one of the CASEs from the list underneath. That list is indented and says:
  584. In the case where a$ is ``fine'', print ``That's good!''
  585. In the case where a$ is ``lousy'', print ``Too bad!''
  586. In the case where a$ is anything else, print ``I feel the same way!''
  587. The bottom line of every SELECT statement must say END SELECT.
  588.  
  589.                                                                 Complete program
  590.     Here's a complete program:
  591. CLS
  592. INPUT "How are you"; a$
  593. SELECT CASE a$
  594.         CASE "fine"
  595.                 PRINT "That's good!"
  596.         CASE "lousy"
  597.                 PRINT "Too bad!"
  598.         CASE ELSE
  599.                 PRINT "I feel the same way!"
  600. END SELECT
  601. PRINT "I hope you enjoyed your therapy.  Now you owe $50."
  602.     Line 2 makes the computer ask the patient, ``How are you?'' The next several lines are the SELECT statement, which makes the computer analyze the patient's answer and print ``That's good!'' or ``Too bad!'' or else ``I feel the same way!''
  603.     Regardless of what the patient and computer said, that program's bottom line always makes the computer end the conversation by printing:
  604. I hope you enjoyed your therapy.  Now you owe $50.
  605.     In that program, try changing the strings to make the computer print smarter remarks, become a better therapist, and charge even more money.                                                                                            Error trap
  606.     This program makes the computer discuss human sexuality:
  607. CLS
  608. 10 INPUT "Are you male or female"; a$
  609. SELECT CASE a$
  610.         CASE "male"
  611.                 PRINT "So is Frankenstein!"
  612.         CASE "female"
  613.                 PRINT "So is Mary Poppins!"
  614.         CASE ELSE
  615.                 PRINT "Please say male or female!"
  616.                 GOTO 10
  617. END SELECT
  618.     The second line (which is numbered 10) makes the computer ask, ``Are you male or female?''
  619.     The remaining lines are a SELECT statement that analyzes the human's response. If the human claims to be ``male'', the computer prints ``So is Frankenstein!'' If the human says ``female'' instead, the computer prints ``So is Mary Poppins!'' If the human says anything else (such as ``not sure'' or ``super-male'' or ``macho'' or ``none of your business''), the computer does the CASE ELSE, which makes the computer say ``Please say male or female!'' and then go back to line 10, which makes the computer ask again, ``Are you male or female?''
  620.     In that program, the CASE ELSE is called an error handler (or error-handling routine or error trap), since its only purpose is to handle human error (a human who says neither ``male'' nor ``female''). Notice that the error handler begins by printing a gripe message (``Please say male or female!'') and then lets the human try again (GOTO 10).
  621.     In QBASIC, the GOTO statements are used rarely: they're used mainly in error handlers, to let the human try again.
  622.     Do you like Mary Poppins? Let's extend that program's conversation. If the human says ``female'', let's make the computer say ``So is Mary Poppins!'', then ask ``Do you like her?'', then continue the conversation as follows:
  623. If the human says ``yes'', make the computer say ``I like her too. She is my mother.''
  624. If the human says ``no'', make the computer say ``I hate her too. She owes me a dime.''
  625. If the human says neither ``yes'' nor ``no'', make the computer handle that error.
  626.     To accomplish all that, insert the shaded lines into the program:
  627. CLS
  628. 10 INPUT "Are you male or female"; a$
  629. SELECT CASE a$
  630.         CASE "male"
  631.                 PRINT "So is Frankenstein!"
  632.         CASE "female"
  633.                 PRINT "So is Mary Poppins!"
  634. 20              INPUT "Do you like her"; b$                                 
  635.                 SELECT CASE b$                                              
  636.                         CASE "yes"                                          
  637.                                 PRINT "I like her too.  She is my mother."  
  638.                         CASE "no"                                           
  639.                                 PRINT "I hate her too.  She owes me a dime."
  640.                         CASE ELSE                                           
  641.                                 PRINT "Please say yes or no!"               
  642.                                 GO TO 20                                    
  643.                 END SELECT                                                  
  644.         CASE ELSE
  645.                 PRINT "Please say male or female!"
  646.                 GOTO 10
  647. END SELECT
  648.                                                                                          Weird programs
  649.     The computer's abilities are limited only by your own imagination ___ and your weirdness. Here are some weird programs from weird minds. . . . 
  650.     Friends Like a human, the computer wants to meet new friends. This program makes the computer show its true feelings:
  651. CLS
  652. 10 INPUT "Are you my friend"; a$
  653. SELECT CASE a$
  654.         CASE "yes"
  655.                 PRINT "That's swell."
  656.         CASE "no"
  657.                 PRINT "Go jump in a lake."
  658.         CASE ELSE
  659.                 PRINT "Please say yes or no."
  660.                 GO TO 10
  661. END SELECT
  662.     When you run that program, the computer asks ``Are you my friend?'' If you say ``yes'', the computer says ``That's swell.'' If you say ``no'', the computer says ``Go jump in a lake.''
  663.     Watch TV The most inventive programmers are kids. This program was written by a girl in the sixth grade:
  664. CLS
  665. 10 INPUT "Can I come over to your house to watch TV"; a$
  666. SELECT CASE a$
  667.         CASE "yes"
  668.                 PRINT "Thanks.  I'll be there at 5PM."
  669.         CASE "no"
  670.                 PRINT "Humph!  Your feet smell, anyway."
  671.         CASE ELSE
  672.                 PRINT "Please say yes or no."
  673.                 GO TO 10
  674. END SELECT
  675.     When you run that program, the computer asks to watch your TV. If you say ``yes'', the computer promises to come to your house at 5. If you refuse, the computer insults your feet.
  676.     Honesty Another sixth-grade girl wrote this program, to test your honesty:
  677. CLS
  678. PRINT "FKGJDFGKJ*#K$JSLF*/#$()$&(IKJNHBGD52:?./KSDJK$E(EF$#/JIK(*"
  679. PRINT "FASDFJKL:JFRFVFJUNJI*&()JNE$#SKI#(!SERF HHW NNWAZ MAME !!!"
  680. PRINT "ZBB%%%%%##)))))FESDFJK DSFE N.D.JJUJASD EHWLKD******"
  681. 10 INPUT "Do you understand what I said"; a$
  682. SELECT CASE a$
  683.         CASE "no"
  684.                 PRINT "Sorry to have bothered you."
  685.         CASE "yes"
  686.                 PRINT "SSFJSLFKDJFL++++45673456779XSDWFEF/#$&**()---==!!ZZXX"
  687.                 PRINT "###EDFHTG NVFDF MKJK ==+--*$&% #RHFS SES DOPEKKK DSBS"
  688.                 INPUT "Okay, what did I say"; b$
  689.                 PRINT "You are a liar, a liar, a big fat liar!"
  690.         CASE ELSE
  691.                 PRINT "Please say yes or no."
  692.                 GO TO 10
  693. END SELECT
  694.     When you run that program, lines 2-4 print nonsense. Then the computer asks whether you understand that stuff. If you're honest and answer ``no'', the computer will apologize. But if you pretend that you understand the nonsense and answer ``yes'', the computer will print more nonsense, challenge you to translate it, wait for you to fake a translation, and then scold you for lying.     A Daddy wrote a program for his five-year-old son, John.
  695.     When John runs the program and types his name, the computer asks ``What's 2 and 2?'' If John answers 4, the computer says ``No, 2 and 2 is 22''. If he runs the program again and answers 22, the computer says ``No, 2 and 2 is 4''. No matter how many times he runs the program and how he answers the question, the computer says he's wrong. But when Daddy runs the program, the computer replies, ``Yes, Daddy is always right''.
  696.     Here's how Daddy programmed the computer:
  697. CLS
  698. INPUT "What's your name"; n$
  699. INPUT "What's 2 and 2"; a
  700. IF n$ = "Daddy" THEN PRINT "Yes, Daddy is always right": END
  701. IF a = 4 THEN PRINT "No, 2 and 2 is 22" ELSE PRINT "No, 2 and 2 is 4"
  702.  
  703.                                                                                        Different relations
  704.     You can make the IF clause very fancy:
  705. IF clause        Meaning
  706. IF b$ = "male"    If b$ is ``male''
  707. IF b = 4            If b is 4
  708. IF b < 4            If b is less than 4
  709. IF b > 4            If b is greater than 4
  710. IF b <= 4            If b is less than or equal to 4
  711. IF b >= 4            If b is greater than or equal to 4
  712. IF b <> 4            If b is not 4
  713. IF b$ < "male"    If b$ is a word that comes before ``male'' in the dictionary
  714. IF b$ > "male"    If b$ is a word that comes after ``male'' in the dictionary
  715.     In the IF statement, the symbols =, <, >, <=, >=, and <> are called relations.
  716.     When writing a relation, mathematicians and computerists habitually put the equal sign last:
  717. Right    Wrong
  718. <=        =<
  719. >=        =>
  720. When you press the ENTER key at the end of the line, the computer will automatically put your equal signs last: the computer will turn any ``=<'' into ``<=''; it will turn any ``=>'' into ``<=''.
  721.     To say ``not equal to'', say ``less than or greater than'', like this: <>.
  722.  
  723.                                                                                                OR
  724.     The computer understands the word OR. For example, here's how to say, ``If x is either 7 or 8, print the word wonderful'':
  725. IF x = 7 OR x = 8 THEN PRINT "wonderful"
  726.     That example is composed of two conditions: the first condition is ``x = 7''; the second condition is ``x = 8''. Those two conditions combine, to form ``x = 7 OR x = 8'', which is called a compound condition.
  727.     If you use the word OR, put it between two conditions.
  728. Right:    IF x = 7 OR x = 8 THEN PRINT "wonderful"    (``x = 7'' and ``x = 8'' are conditions.)
  729. Wrong:    IF x = 7 OR 8 THEN PRINT "wonderful"            (``8'' is not a condition.)
  730.  
  731.                                                                                                AND
  732.     The computer understands the word AND. Here's how to say, ``If p is more than 5 and less than 10, print tuna fish'':
  733. IF p > 5 AND p < 10 THEN PRINT "tuna fish"
  734. Here's how to say, ``If s is at least 60 and less than 65, print you almost failed'':
  735. IF s >= 60 AND s < 65 THEN PRINT "you almost failed"
  736. Here's how to say, ``If n is a number from 1 to 10, print that's good'':
  737. IF n >= 1 AND n <= 10 THEN PRINT "that's good"
  738.                                                         Can a computer become President?
  739.     To become President of the United States, you need four basic skills.
  740.     First, you must be a good talker, so you can give effective speeches saying ``Vote for me!'', express your views, and make folks do what you want.
  741.     But even if you're a good talker, you're useless unless you're also a good listener. You must be able to listen to people's needs and ask, ``What can I do to make you happy and get you to vote for me?''
  742.     But even if you're a good talker and listener, you're still useless unless you can make decisions. Should you give more money to poor people? Should you bomb the enemy? Which actions should you take, and under what conditions?
  743.     But even if you're a good talker and listener and decision maker, you still need one more trait to become President: you must be able to take the daily grind of politics. You must, again and again, shake hands, make compromises, and raise funds. You must have the patience to put up with the repetitive monotony of those chores.
  744.     So altogether, to become President you need to be a good talker and listener and decision maker and also have the patience to put up with monotonous repetition.
  745.     Those are exactly the four qualities the computer has! The word PRINT turns the computer into a good speech-maker: by using the word PRINT, you can make the computer write whatever speech you wish. The word INPUT turns the computer into a good listener: by using the word INPUT, you can make the computer ask humans lots of questions, to find out who the humans are and what they want. The word IF turns the computer into a decision maker: the computer can analyze the IF condition, determine whether that condition is true, and act accordingly. Finally, the word GOTO enables the computer to perform loops, which the computer will repeat patiently.
  746.     So by using the words PRINT, INPUT, IF, and GOTO, you can make the computer imitate any intellectual human activity. Those four magic words ___ PRINT, INPUT, IF, and GOTO ___ are the only concepts you need, to write whatever program you wish!
  747.     Yes, you can make the computer imitate the President of the United States, do your company's payroll, compose a beautiful poem, play a perfect game of chess, contemplate the meaning of life, act as if it's falling in love, or do whatever other intellectual or emotional task you wish, by using those four magic words. The only question is: how? The Secret Guide to Computers teaches you how, by showing you many examples of programs that do those remarkable things.     What programmers believe Yes, we programmers believe that all of life can be explained and programmed. We believe all of life can be reduced to just those four concepts: PRINT, INPUT, IF, and GOTO. Programming is the ultimate act of scientific reductionism: programmers reduce all of life scientifically to just four concepts.
  748.     The words that the computer understands are called keywords. The four essential keywords are PRINT, INPUT, IF, and GOTO.
  749.     The computer also understands extra keywords, such as CLS, LPRINT, WIDTH, SYSTEM, SLEEP, DO (and LOOP), END, SELECT (and CASE), and words used in IF statements (such as THEN, ELSE, ELSEIF, OR, AND). Those extra keywords aren't necessary: if they hadn't been invented, you could still write programs without them. But they make programming easier.
  750.     A BASIC programmer is a person who translates an ordinary English sentence (such as ``act like the President'' or ``do the payroll'') into a series of BASIC statements, using keywords such as PRINT, INPUT, IF, GOTO, CLS, etc.
  751.     The mysteries of life Let's dig deeper into the mysteries of PRINT, INPUT, IF, GOTO, and the extra keywords. The deeper we dig, the more you'll wonder: are you just a computer, made of flesh instead of wires? Can everything that you do be explained in terms of PRINT, INPUT, IF, and GOTO?
  752.     By the time you finish The Secret Guide to Computers, you'll know!     This program plays a guessing game, where the human tries to guess the computer's favorite color, which is pink:
  753. CLS
  754. 10 INPUT "What's my favorite color"; guess$
  755. IF guess$ = "pink" THEN
  756.         PRINT "Congratulations!  You discovered my favorite color."
  757. ELSE
  758.         PRINT "No, that's not my favorite color.  Try again!"
  759.         GOTO 10
  760. END IF
  761.     The INPUT line asks the human to guess the computer's favorite color; the guess is called guess$.
  762.     If the guess is ``pink'', the computer prints:
  763. Congratulations!  You discovered my favorite color.
  764. But if the guess is not ``pink'', the computer will instead print ``No, that's not my favorite color'' and then GO back TO line 10, which asks the human again to try guessing the computer's favorite color.
  765.  
  766.                                                                                                END
  767.     Here's how to write that program without saying GOTO:
  768. CLS
  769. DO
  770.         INPUT "What's my favorite color"; guess$
  771.         IF guess$ = "pink" THEN
  772.                 PRINT "Congratulations!  You discovered my favorite color."
  773.                 END
  774.         END IF
  775.         PRINT "No, that's not my favorite color.  Try again!"
  776. LOOP
  777.     That new version of the program contains a DO loop. That loop makes the computer do this repeatedly: ask ``What's my favorite color?'' and then PRINT ``No, that's not my favorite color.''
  778.     The only way to stop the loop is to guess ``pink'', which makes the computer print ``Congratulations!'' and END.
  779.  
  780.                                                                                              EXIT DO
  781.     Here's another way to write that program without saying GOTO:
  782. CLS
  783. DO
  784.         INPUT "What's my favorite color"; guess$
  785.         IF guess$ = "pink" THEN EXIT DO
  786.         PRINT "No, that's not my favorite color.  Try again!"
  787. LOOP
  788. PRINT "Congratulations!  You discovered my favorite color."
  789.     That program's DO loop makes the computer do this repeatedly: ask ``What's my favorite color?'' and then PRINT ``No, that's not my favorite color.''
  790.     The only way to stop the loop is to guess ``pink'', which makes the computer EXIT from the DO loop; then the computer proceeds to the line underneath the DO loop. That line prints:
  791. Congratulations!  You discovered my favorite color.
  792.  
  793.                                                                                            LOOP UNTIL
  794.     Here's another way to program the guessing game:
  795. CLS
  796. DO
  797.         PRINT "You haven't guessed my favorite color yet!"
  798.         INPUT "What's my favorite color"; guess$
  799. LOOP UNTIL guess$ = "pink"
  800. PRINT "Congratulations!  You discovered my favorite color."
  801.     That program's DO loop makes the computer do this repeatedly: say ``You haven't guessed my favorite color yet!'' and then ask ``What's my favorite color?''
  802.     The LOOP line makes the computer repeat the indented lines again and again, UNTIL the guess is ``pink''. When the guess is ``pink'', the computer proceeds to the line underneath the LOOP and prints ``Congratulations!''.
  803.     Achieving the goal The LOOP UNTIL's condition (guess$ = ``pink'') is called the loop's goal. The computer does the loop repeatedly, until the loop's goal is achieved. Here's how. . . . 
  804.     The computer does the indented lines, then checks whether the goal is achieved yet. If the goal is not achieved yet, the computer does the indented lines again, then checks again whether the goal is achieved. The computer does the loop again and again, until the goal is achieved. Then the computer, proud at achieving the goal, does the program's finale, which consists of any lines under the LOOP UNTIL line.
  805.     UNTIL versus EXIT Saying ___ 
  806. LOOP UNTIL guess$ = "pink"
  807. is just a briefer way of saying this pair of lines:
  808.         IF guess$ = "pink" THEN EXIT DO
  809. LOOP
  810.     Let's make the computer print every number from 1 to 20, like this:
  811.  1
  812.  2
  813.  3
  814.  4
  815.  5
  816.  6
  817.  7
  818.  etc.
  819.  20
  820.     Here's the program:
  821. CLS
  822. FOR x = 1 TO 20
  823.         PRINT x
  824. NEXT
  825. The second line (FOR x = 1 TO 20) says that x will be every number from 1 to 20; so x will be 1, then 2, then 3, etc. The line underneath, which is indented, says what to do about each x; it says to PRINT each x.
  826.     Whenever you write a program that contains the word FOR, you must say NEXT; so the bottom line says NEXT.
  827.     The indented line, which is between the FOR line and the NEXT line, is the line that the computer will do repeatedly; so the computer will repeatedly PRINT x. The first time the computer prints x, the x will be 1, so the computer will print:
  828.  1
  829. The next time the computer prints x, the x will be 2, so the computer will print:
  830.  2
  831. The computer will print every number from 1 up to 20.
  832.  
  833.                                                                When men meet women
  834.     Let's make the computer print these lyrics:
  835. I saw 2 men
  836. meet 2 women.
  837. Tra-la-la!
  838.  
  839. I saw 3 men
  840. meet 3 women.
  841. Tra-la-la!
  842.  
  843. I saw 4 men
  844. meet 4 women.
  845. Tra-la-la!
  846.  
  847. I saw 5 men
  848. meet 5 women.
  849. Tra-la-la!
  850.  
  851. They all had a party!
  852. Ha-ha-ha!
  853.     To do that, type these lines ___ 
  854. The first line of each verse:        PRINT "I saw"; x; "men"
  855. The second line of each verse:    PRINT "meet"; x; "women."
  856. The third line of each verse:        PRINT "Tra-la-la!"
  857. Blank line under each verse:        PRINT
  858. and make x be every number from 2 up to 5:
  859. FOR x = 2 TO 5
  860.         PRINT "I saw"; x; "men"
  861.         PRINT "meet"; x; "women."
  862.         PRINT "Tra-la-la!"
  863.         PRINT
  864. NEXT
  865.     At the top of the program, say CLS. At the end of the song, print the closing couplet:
  866. CLS
  867. FOR x = 2 TO 5
  868.         PRINT "I saw"; x; "men"
  869.         PRINT "meet"; x; "women."
  870.         PRINT "Tra-la-la!"
  871.         PRINT
  872. NEXT
  873. PRINT "They all had a party!"
  874. PRINT "Ha-ha-ha!"            
  875. That program makes the computer print the entire song.
  876.     Here's an analysis:
  877.                                 CLS
  878.                                 FOR X = 2 TO 5
  879. The computer will do the                PRINT "I saw"; x; "men"
  880. indented lines repeatedly,                PRINT "meet"; x; "women."
  881. for x=2, x=3, x=4, and x=5.            PRINT "Tra-la-la!"
  882.                                         PRINT
  883.                                 NEXT
  884. Then the computer will            PRINT "They all had a party!"
  885. print this couplet once.            PRINT "Ha-ha-ha!"
  886.     Since the computer does the indented lines repeatedly, those lines form a loop. Here's the general rule: the statements between FOR and NEXT form a loop. The computer goes round and round the loop, for x=2, x=3, x=4, and x=5. Altogether, it goes around the loop 4 times, which is a finite number. Therefore, the loop is finite.
  887.     If you don't like the letter x, choose a different letter. For example, you can choose the letter i:
  888. CLS
  889. FOR i = 2 TO 5
  890.         PRINT "I saw"; i; "men"
  891.         PRINT "meet"; i; "women."
  892.         PRINT "Tra-la-la!"
  893.         PRINT
  894. NEXT
  895. PRINT "They all had a party!"
  896. PRINT "Ha-ha-ha!"
  897.     When using the word FOR, most programmers prefer the letter i; most programmers say ``FOR i'' instead of ``FOR x''. Saying ``FOR i'' is an ``old tradition''. Following that tradition, the rest of this book says ``FOR i'' (instead of ``FOR x''), except in situations where some other letter feels more natural.
  898.  
  899.                                                                                                                                                                                                                             Print the squares
  900.     To find the square of a number, multiply the number by itself. The square of 3 is ``3 times 3'', which is 9. The square of 4 is ``4 times 4'', which is 16.
  901.     Let's make the computer print the square of 3, 4, 5, etc., up to 20, like this:
  902. The square of 3 is 9
  903. The square of 4 is 16
  904. The square of 5 is 25
  905. The square of 6 is 36
  906. The square of 7 is 49
  907. etc.
  908. The square of 20 is 400
  909.     To do that, type this line ___ 
  910.         PRINT "The square of"; i; "is"; i*i
  911. and make i be every number from 3 up to 20, like this:
  912. CLS
  913. FOR i = 3 TO 20
  914.         PRINT "The square of"; i; "is"; i*i
  915. NEXT
  916.                                                               Count how many copies
  917.     This program, which you saw before, prints ``love'' on every line of your screen:
  918. CLS
  919. DO
  920.         PRINT "love"
  921. LOOP
  922. That program prints ``love'' again and again, until you abort the program by pressing Ctrl with PAUSE/BREAK.
  923.     But what if you want to print ``love'' just 20 times? This program prints ``love'' just 20 times:
  924. CLS
  925. FOR i = 1 TO 20
  926.         PRINT "love"
  927. NEXT
  928.     As you can see, FOR...NEXT resembles DO...LOOP but is smarter: while doing FOR...NEXT, the computer counts!
  929.     Poem This program, which you saw before, prints many copies of a poem:
  930. CLS
  931. DO
  932.         LPRINT "I'm having trouble"
  933.         LPRINT "With my nose."
  934.         LPRINT "The only thing it does is:"
  935.         LPRINT "Blows!"
  936.         LPRINT CHR$(12);
  937. LOOP
  938. It prints the copies onto paper. It prints each copy on a separate sheet of printer. It keeps printing until you abort the program ___ or the printer runs out of paper.
  939.     Here's a smarter program, which counts the number of copies printed and stops when exactly 4 copies have been printed:
  940. CLS
  941. FOR i = 1 TO 4
  942.         LPRINT "I'm having trouble"
  943.         LPRINT "With my nose."
  944.         LPRINT "The only thing it does is:"
  945.         LPRINT "Blows!"
  946.         LPRINT CHR$(12);
  947. NEXT
  948. It's the same as the DO...LOOP program, except that it counts (by saying ``FOR i = 1 TO 4'' instead of ``DO'') and has a different bottom line (NEXT instead of LOOP).
  949.     Here's an even smarter program, which asks how many copies you want:
  950. CLS
  951. INPUT "How many copies of the poem do you want"; n
  952. FOR i = 1 TO n
  953.         LPRINT "I'm having trouble"
  954.         LPRINT "With my nose."
  955.         LPRINT "The only thing it does is:"
  956.         LPRINT "Blows!"
  957.         LPRINT CHR$(12);
  958. NEXT
  959. When you run that program, the computer asks:
  960. How many copies of the poem do you want?
  961. If you answer 5, then the n becomes 5 and so the computer prints 5 copies of the poem. If you answer 7 instead, the computer prints 7 copies. Print as many copies as you like!
  962.     That program illustrates this rule:
  963. To make the FOR...NEXT loop flexible,
  964. say ``FOR i = 1 TO n'' and let the human INPUT the n.
  965.                                                                 Count to midnight
  966.     This program makes the computer count to midnight:
  967. CLS
  968. FOR i = 1 TO 11
  969.         PRINT i
  970. NEXT
  971. PRINT "midnight"
  972.     The computer will print:
  973.  1
  974.  2
  975.  3
  976.  4
  977.  5
  978.  6
  979.  7
  980.  8
  981.  9
  982.  10
  983.  11
  984. midnight
  985.     Semicolon Let's put a semicolon at the end of the indented line:
  986. CLS
  987. FOR i = 1 TO 11
  988.         PRINT i;
  989. NEXT
  990. PRINT "midnight"
  991. The semicolon makes the computer print each item on the same line, like this:
  992.  1  2  3  4  5  6  7  8  9  10  11 midnight
  993.     If you want the computer to press the ENTER key before ``midnight'', insert a PRINT line:
  994. CLS
  995. FOR i = 1 TO 11
  996.         PRINT i;
  997. NEXT
  998. PRINT
  999. PRINT "midnight"
  1000. That extra PRINT line makes the computer press the ENTER key just before ``midnight'', so the computer will print ``midnight'' on a separate line, like this:
  1001.  1  2  3  4  5  6  7  8  9  10  11
  1002. midnight
  1003.     Nested loops Let's make the computer count to midnight 3 times, like this:
  1004.  1  2  3  4  5  6  7  8  9  10  11
  1005. midnight
  1006.  1  2  3  4  5  6  7  8  9  10  11
  1007. midnight
  1008.  1  2  3  4  5  6  7  8  9  10  11
  1009. midnight
  1010. To do that, put the entire program between the words FOR and NEXT:
  1011. CLS
  1012. FOR j = 1 TO 3
  1013.         FOR i = 1 TO 11
  1014.             PRINT i;
  1015.         NEXT
  1016.         PRINT
  1017.         PRINT "midnight"
  1018. NEXT    
  1019.     That version contains a loop inside a loop: the loop that says ``FOR i'' is inside the loop that says ``FOR j''. The j loop is called the outer loop; the i loop is called the inner loop. The inner loop's variable must differ from the outer loop's. Since we called the inner loop's variable ``i'', the outer loop's variable must not be called ``i''; so I picked the letter j instead.
  1020.     Programmers often think of the outer loop as a bird's nest, and the inner loop as an egg inside the nest. So programmers say the inner loop is nested in the outer loop; the inner loop is a nested loop.                                                                                           Abnormal exit
  1021.     Earlier, we programmed a game where the human tries to guess the computer's favorite color, pink. Here's a fancier version of the game, in which the human gets just 5 guesses:
  1022. CLS
  1023. PRINT "I'll give you 5 guesses...."
  1024. FOR i = 1 TO 5
  1025.         INPUT "What's my favorite color"; guess$
  1026.         IF guess$ = "pink" THEN GO TO 10
  1027.         PRINT "No, that's not my favorite color."
  1028. NEXT
  1029. PRINT "Sorry, your 5 guesses are up!  You lose."
  1030. END
  1031.  
  1032. 10 PRINT "Congratulations!  You discovered my favorite color."
  1033. PRINT "It took you"; i; "guesses."
  1034.     Line 2 warns the human that just 5 guesses are allowed. The FOR line makes the computer count from 1 to 5; to begin, i is 1. The INPUT line asks the human to guess the computer's favorite color; the guess is called guess$.
  1035.     If the guess is ``pink'', the computer jumps down to the line numbered 10, prints ``Congratulations!'', and tells how many guesses the human took. But if the guess is not ``pink'', the computer will print ``No, that's not my favorite color'' and go on to the NEXT guess.
  1036.     If the human guesses 5 times without success, the computer proceeds to the line that prints ``Sorry . . . You lose.''
  1037.     For example, if the human's third guess is ``pink'', the computer prints:
  1038. Congratulations!  You discovered my favorite color.
  1039. It took you 3 guesses.
  1040.     If the human's very first guess is ``pink'', the computer prints:
  1041. Congratulations!  You discovered my favorite color.
  1042. It took you 1 guesses.
  1043. Saying ``1 guesses'' is bad grammar but understandable.
  1044.     That program contains a FOR...NEXT loop. The FOR line says the loop will normally be done five times. The line below the loop (which says to PRINT ``Sorry'') is the loop's normal exit. But if the human happens to input ``pink'', the computer jumps out of the loop early, to line 10, which is the loop's abnormal exit.
  1045.  
  1046.                                                                                               STEP
  1047.     The FOR statement can be varied:
  1048. Statement                    Meaning
  1049. FOR i = 5 TO 17 STEP .1    The i will go from 5 to 17, counting by tenths.
  1050.                                 So i will be 5, then 5.1, then 5.2, etc., up to 17.
  1051.  
  1052. FOR i = 5 TO 17 STEP 3        The i will be every third number from 5 to 17.
  1053.                                 So i will be 5, then 8, then 11, then 14, then 17.
  1054.  
  1055. FOR i = 17 TO 5 STEP -3    The i will be every third number from 17 down to 5.
  1056.                                 So i will be 17, then 14, then 11, then 8, then 5.
  1057.     To count down, you must use the word STEP. To count from 17 down to 5, give this instruction:
  1058. FOR i = 17 TO 5 STEP -1
  1059.     This program prints a rocket countdown:
  1060. CLS
  1061. FOR i = 10 TO 1 STEP -1
  1062.         PRINT i
  1063. NEXT
  1064. PRINT "Blast off!"
  1065. The computer will print:
  1066.  10
  1067.  9
  1068.  8
  1069.  7
  1070.  6
  1071.  5
  1072.  4
  1073.  3
  1074.  2
  1075.  1
  1076. Blast off!     This statement is tricky:
  1077. FOR i = 5 TO 16 STEP 3
  1078. It says to start i at 5, and keep adding 3 until it gets past 16. So i will be 5, then 8, then 11, then 14. The i won't be 17, since 17 is past 16. The first value of i is 5; the last value is 14.
  1079.     In the statement FOR i = 5 TO 16 STEP 3, the first value or initial value of i is 5, the limit value is 16, and the step size or increment is 3. The i is called the counter or index or loop-control variable. Although the limit value is 16, the last value or terminal value is 14.
  1080.     Programmers usually say ``FOR i'', instead of ``FOR x'', because the letter i reminds them of the word index.     Let's make the computer print this message:
  1081. I love meat
  1082. I love potatoes
  1083. I love lettuce
  1084. I love tomatoes
  1085. I love butter
  1086. I love cheese
  1087. I love onions
  1088. I love peas
  1089.     That message concerns this list of food: meat, potatoes, lettuce, tomatoes, butter, cheese, onions, peas. That list doesn't change: the computer continues to love those foods throughout the entire program.
  1090.     A list that doesn't change is called DATA. So in the message about food, the DATA is meat, potatoes, lettuce, tomatoes, butter, cheese, onions, peas.
  1091.     Whenever a problem involves DATA, put the DATA at the top of the program, just under the CLS, like this:
  1092. CLS
  1093. DATA meat,potatoes,lettuce,tomatoes,butter,cheese,onions,peas
  1094.     You must tell the computer to READ the DATA:
  1095. CLS
  1096. DATA meat,potatoes,lettuce,tomatoes,butter,cheese,onions,peas
  1097. READ a$
  1098. That READ line makes the computer read the first datum (``meat'') and call it a$. So a$ is ``meat''.
  1099.     Since a$ is ``meat'', this shaded line makes the computer print ``I love meat'':
  1100. CLS
  1101. DATA meat,potatoes,lettuce,tomatoes,butter,cheese,onions,peas
  1102. READ a$
  1103. PRINT "I love "; a$
  1104.     Hooray! We made the computer handle the first datum correctly: we made the computer print ``I love meat''.
  1105.     To make the computer handle the rest of the data (potatoes, lettuce, etc.), tell the computer to READ and PRINT the rest of the data, by putting the READ and PRINT lines in a loop. Since we want the computer to READ and PRINT all 8 data items (meat, potatoes, lettuce, tomatoes, butter, cheese, onions, peas), put the READ and PRINT lines in a loop that gets done 8 times, by making the loop say ``FOR i = 1 TO 8'':
  1106. CLS
  1107. DATA meat,potatoes,lettuce,tomatoes,butter,cheese,onions,peas
  1108. FOR i = 1 TO 8
  1109.         READ a$
  1110.         PRINT "I love "; a$
  1111. NEXT    
  1112. Since that loop's main purpose is to READ the data, it's called a READ loop.
  1113.     When writing that program, make sure the FOR line's last number (8) is the number of data items. If the FOR line accidentally says 7 instead of 8, the computer won't read or print the 8th data item. If the FOR line accidentally says 9 instead of 8, the computer will try to read a 9th data item, realize that no 9th data item exists, and gripe by saying:
  1114. Out of DATA
  1115. Then press ENTER.     Let's make the computer end by printing ``Those are the foods I love'', like this:
  1116. I love meat
  1117. I love potatoes
  1118. I love lettuce
  1119. I love tomatoes
  1120. I love butter
  1121. I love cheese
  1122. I love onions
  1123. I love peas
  1124. Those are the foods I love
  1125. To make the computer print that ending, put a PRINT line at the end of the program:
  1126. CLS
  1127. DATA meat,potatoes,lettuce,tomatoes,butter,cheese,onions,peas
  1128. FOR i = 1 TO 8
  1129.         READ a$
  1130.         PRINT "I love "; a$
  1131. NEXT
  1132. PRINT "Those are the foods I love"
  1133.  
  1134.                                                                                             End mark
  1135.     When writing that program, we had to count the DATA items and put that number (8) at the end of the FOR line.
  1136.     Here's a better way to write the program, so you don't have to count the DATA items:
  1137. CLS
  1138. DATA meat,potatoes,lettuce,tomatoes,butter,cheese,onions,peas
  1139. DATA end
  1140. DO
  1141.         READ a$: IF a$ = "end" THEN EXIT DO
  1142.         PRINT "I love "; a$
  1143. LOOP
  1144. PRINT "Those are the foods I love"
  1145.     The third line (DATA end) is called the end mark, since it marks the end of the DATA. The READ line means:
  1146. READ a$ from the DATA; but if a$ is the ``end'' of the DATA, then EXIT from the DO loop.
  1147. When the computer exits from the DO loop, the computer prints ``Those are the foods I love''. So altogether, the entire program makes the computer print:
  1148. I love meat
  1149. I love potatoes
  1150. I love lettuce
  1151. I love tomatoes
  1152. I love butter
  1153. I love cheese
  1154. I love onions
  1155. I love peas
  1156. Those are the foods I love
  1157.     The routine that says:
  1158.                  IF a$ = "end" THEN EXIT DO
  1159. is called the end routine, because the computer does that routine when it reaches the end of the DATA.                                                                                         Henry the Eighth
  1160.     Let's make the computer print this nursery rhyme:
  1161. I love ice cream
  1162. I love red
  1163. I love ocean
  1164. I love bed
  1165. I love tall grass
  1166. I love to wed
  1167.  
  1168. I love candles
  1169. I love divorce
  1170. I love kingdom
  1171. I love my horse
  1172. I love you
  1173. Of course, of course,
  1174. For I am Henry the Eighth!
  1175.     If you own a jump rope, have fun: try to recite that poem while skipping rope!
  1176.     This program makes the computer recite the poem:
  1177. CLS
  1178. DATA ice cream,red,ocean,bed,tall grass,to wed
  1179. DATA candles,divorce,my kingdom,my horse,you
  1180. DATA end
  1181. DO
  1182.         READ a$: IF a$ = "end" THEN EXIT DO
  1183.         PRINT "I love "; a$
  1184.         IF a$ = "to wed" THEN PRINT
  1185. LOOP
  1186. PRINT "Of course, of course,"
  1187. PRINT "For I am Henry the Eighth!"
  1188.     Since the data's too long to fit on a single line, I've put part of the data in line 2 and the rest in line 3. Each line of data must begin with the word DATA. In each line, put commas between the items. Do not put a comma at the end of the line.
  1189.     The program resembles the previous one. The new line (IF a$ = ``to wed'' THEN PRINT) makes the computer leave a blank line underneath ``to wed'', to mark the bottom of the first verse.
  1190.  
  1191.                                                                                           Pairs of data
  1192.     Let's throw a party! To make the party yummy, let's ask each guest to bring a kind of food that resembles the guest's name. For example, let's have Sal bring salad, Russ bring Russian dressing, Sue bring soup, Tom bring turkey, Winnie bring wine, Kay bring cake, and Al bring Alka-Seltzer.
  1193.     Let's send all those people invitations, in this form:
  1194. Dear _____________,
  1195.      person's name
  1196.  
  1197.      We're throwing a party in the clubhouse at midnight!
  1198.  
  1199. Please bring ____.
  1200.              food
  1201.     Here's the program:
  1202. CLS
  1203. DATA Sal,salad,Russ,Russian dressing,Sue,soup,Tom,turkey
  1204. DATA Winnie,wine,Kay,cake,Al,Alka-Seltzer
  1205. DATA end,end
  1206. DO
  1207.         READ person$, food$: IF person$ = "end" THEN EXIT DO
  1208.         LPRINT "Dear "; person$; ","
  1209.         LPRINT "     We're throwing a party in the clubhouse at midnight!"
  1210.         LPRINT "Please bring "; food$; "."
  1211.         LPRINT CHR$(12);
  1212. LOOP
  1213. PRINT "I've finished writing the letters."
  1214.     The DATA comes in pairs. For example, the first pair consists of ``Sal'' and ``salad''; the next pair consists of ``Russ'' and ``Russian dressing''. Since the DATA comes in pairs, you must make the end mark also be a pair (DATA end,end).     Since the DATA comes in pairs, the READ line says to READ a pair of data (person$ and food$). The first time that the computer encounters the READ line, person$ is ``Sal''; food$ is ``salad''. Then the LPRINT lines print this message onto paper:
  1215. Dear Sal,
  1216.      We're throwing a party in the clubhouse at midnight!
  1217. Please bring salad.
  1218. The LPRINT CHR$(12) makes the computer eject the paper from the printer.
  1219.     Then the computer comes to the word LOOP, which sends the computer back to the word DO, which sends the computer to the READ line again, which reads the next pair of DATA, so person$ becomes ``Russ'' and food$ becomes ``Russian dressing''. The LPRINT lines print onto paper:
  1220. Dear Russ,
  1221.      We're throwing a party in the clubhouse at midnight!
  1222. Please bring Russian dressing.
  1223.     The computer prints similar letters to all the people.
  1224.     After all people have been handled, the READ statement comes to the end mark (DATA end,end), so that person$ and food$ both become ``end''. Since person$ is ``end'', the IF statement makes the computer EXIT DO, so the computer prints this message onto the screen:
  1225. I've finished writing the letters.
  1226.     In that program, you need two ends to mark the data's ending, because the READ statment says to read two strings (person$ and food$).
  1227.     Debts Suppose these people owe you things:
  1228. Person    What the person owes
  1229. Bob        $537.29
  1230. Mike        a dime
  1231. Sue        2 golf balls
  1232. Harry        a steak dinner at Mario's
  1233. Mommy    a kiss
  1234.     Let's remind those people of their debt, by writing them letters, in this form:
  1235. Dear _____________,
  1236.      person's name
  1237.  
  1238.      I just want to remind you...
  1239.  
  1240. that you still owe me ____.
  1241.                       debt
  1242.     To start writing the program, begin by saying CLS and then feed the computer the DATA. The final program is the same as the previous program, except for the part I've shaded:
  1243. CLS
  1244. DATA Bob,$537.29,Mike,a dime,Sue,2 golf balls
  1245. DATA Harry,a steak dinner at Mario's,Mommy,a kiss
  1246. DATA end,end
  1247. DO
  1248.         READ person$, debt$: IF person$ = "end" THEN EXIT DO
  1249.         LPRINT "Dear "; person$; ","
  1250.         LPRINT "     I just want to remind you..."
  1251.         LPRINT "that you still owe me "; debt$; "."
  1252.         LPRINT CHR$(12);
  1253. LOOP
  1254. PRINT "I've finished writing the letters."
  1255.                                                                                               Diets
  1256.     Suppose you're running a diet clinic and get these results:
  1257. Person    Weight before    Weight after
  1258. Joe            273 pounds            219 pounds
  1259. Mary        412 pounds            371 pounds
  1260. Bill        241 pounds            173 pounds
  1261. Sam        309 pounds            198 pounds
  1262.     This program makes the computer print a nice report:
  1263. CLS
  1264. DATA Joe,273,219,Mary,412,371,Bill,241,173,Sam,309,198
  1265. DATA end,0,0
  1266. DO
  1267.         READ person$, weight.before, weight.after
  1268.         IF person$ = "end" THEN EXIT DO
  1269.         PRINT person$; " weighed"; weight.before;
  1270.         PRINT "pounds before attending the diet clinic"
  1271.         PRINT "but weighed just"; weight.after; "pounds afterwards."
  1272.         PRINT "That's a loss of"; weight.before - weight.after; "pounds."
  1273.         PRINT
  1274. LOOP
  1275. PRINT "Come to the diet clinic!"
  1276.     Line 2 contains the DATA, which comes in triplets. The first triplet consists of Joe, 273, and 219. Each triplet includes a string (such as Joe) and two numbers (such as 273 and 219), so line 3's end mark also includes a string and two numbers: it's the word ``end'' and two zeros. (If you hate zeros, you can use other numbers instead; but most programmers prefer zeros.)
  1277.     The READ line says to read a triplet: a string (person$) and two numbers (weight.before and weight.after). The first time the computer comes to the READ statement, the computer makes person$ be ``Joe'', weight.before be 273, and weight.after be 219. The PRINT lines print this:
  1278. Joe weighed 273 pounds before attending the diet clinic
  1279. but weighed just 219 pounds afterwards.
  1280. That's a loss of 54 pounds.
  1281.  
  1282. Mary weighed 412 pounds before attending the diet clinic
  1283. but weighed just 371 pounds afterwards.
  1284. That's a loss of 41 pounds.
  1285.  
  1286. Bill weighed 241 pounds before attending the diet clinic
  1287. but weighed just 173 pounds afterwards.
  1288. That's a loss of 68 pounds.
  1289.  
  1290. Sam weighed 309 pounds before attending the diet clinic
  1291. but weighed just 198 pounds afterwards.
  1292. That's a loss of 111 pounds.
  1293.  
  1294. Come to the diet clinic!
  1295.                                              RESTORE
  1296.     Examine this program:
  1297. CLS
  1298. DATA love,death,war
  1299. 10 DATA chocolate,strawberry
  1300. READ a$
  1301. PRINT a$
  1302. RESTORE 10
  1303. READ a$
  1304. PRINT a$
  1305.     The first READ makes the computer read the first datum (love), so the first PRINT makes the computer print:
  1306. love
  1307.     The next READ would normally make the computer read the next datum (death); but the RESTORE 10 tells the READ to skip ahead to DATA line 10, so the READ line reads ``chocolate'' instead. The entire program prints:
  1308. love
  1309. chocolate
  1310.     So saying ``RESTORE 10'' makes the next READ skip ahead to DATA line 10. If you write a new program, saying ``RESTORE 20'' makes the next READ skip ahead to DATA line 20. Saying just ``RESTORE'' makes the next READ skip back to the beginning of the first DATA line.     Continents This program prints the names of the continents:
  1311. CLS
  1312. DATA Europe,Asia,Africa,Australia,Antarctica,North America,South America
  1313. DATA end
  1314. DO
  1315.         READ a$: IF a$ = "end" THEN EXIT DO
  1316.         PRINT a$
  1317. LOOP
  1318. PRINT "Those are the continents."
  1319. That program makes the computer print this message:
  1320. Europe
  1321. Asia
  1322. Africa
  1323. Australia
  1324. Antarctica
  1325. North America
  1326. South America
  1327. Those are the continents.
  1328.     Let's make the computer print that message twice, so the computer prints:
  1329. Europe
  1330. Asia
  1331. Africa
  1332. Australia
  1333. Antarctica
  1334. North America
  1335. South America
  1336. Those are the continents.
  1337.  
  1338. Europe
  1339. Asia
  1340. Africa
  1341. Australia
  1342. Antarctica
  1343. North America
  1344. South Ameruca
  1345. Those are the continents.
  1346.     To do that, put the program in a loop saying ``FOR i = 1 TO 2'', like this:
  1347. CLS
  1348. DATA Europe,Asia,Africa,Australia,Antarctica,North America,South America
  1349. DATA end
  1350. FOR i = 1 TO 2
  1351.         DO
  1352.                 READ a$: IF a$ = "end" THEN EXIT DO
  1353.                 PRINT a$
  1354.         LOOP
  1355.         PRINT "Those are the continents."
  1356.         PRINT
  1357.         RESTORE
  1358. NEXT
  1359.     After that program says to PRINT ``Those are the continents'', the program says to PRINT a blank line and then RESTORE. The word RESTORE makes the READ go back to the beginning of the DATA, so the computer can READ and PRINT the DATA a second time without saying ``Out of DATA''.                                                                    Search loop
  1360.     Let's make the computer translate colors into French. For example, if the human says ``red'', we'll make the computer say the French equivalent, which is:
  1361. rouge
  1362.     Let's make the computer begin by asking ``Which color interests you?'', then wait for the human to type a color (such as ``red''), then reply:
  1363. In French, it's rouge
  1364.     The program begins simply:
  1365. CLS
  1366. INPUT "Which color interests you"; request$
  1367.     Next, we must make the computer translate the requested color into French. To do so, feed the computer this English-French dictionary:
  1368. English    French
  1369. white        blanc
  1370. yellow        jaune
  1371. orange        orange
  1372. red            rouge
  1373. green        vert
  1374. blue        bleu
  1375. brown        brun
  1376. black        noir
  1377.     That dictionary becomes the data:
  1378. CLS
  1379. DATA white,blanc,yellow,jaune,orange,orange,red,rouge
  1380. DATA green,vert,blue,bleu,brown,brun,black,noir      
  1381. INPUT "Which color interests you"; request$
  1382.     The data comes in pairs; each pair consists of an English word (such as ``white'') followed by its French equivalent (``blanc''). To make the computer read a pair, say:
  1383. READ english$, french$
  1384. To let the computer look at all the pairs, put that READ statement in a DO loop. Here's the complete program:
  1385. CLS
  1386. DATA white,blanc,yellow,jaune,orange,orange,red,rouge
  1387. DATA green,vert,blue,bleu,brown,brun,black,noir
  1388. INPUT "Which color interests you"; request$
  1389. DO                                         
  1390.         READ english$, french$             
  1391.         IF english$ = request$ THEN EXIT DO
  1392. LOOP                                       
  1393. PRINT "In French, it's "; french$          
  1394.     Since the READ line is in a DO loop, the computer does the READ line repeatedly. So the computer keeps READing pairs of DATA, until the computer find the pair of DATA that the human requested. For example, if the human requested ``red'', the computer keeps READing pairs of DATA until it finds a pair whose English word matches the requested word (``red''). When the computer finds that match, the english$ is equal to the request$, so the IF line makes the computer EXIT DO and PRINT:
  1395. In French, it's rouge
  1396.     So altogether, when you run the program the chat can look like this:
  1397. Which color interests you? red
  1398. In French, it's rouge
  1399. Here's another sample run:
  1400. Which color interests you? brown
  1401. In French, it's brun
  1402. Here's another:
  1403. Which color interests you? pink
  1404. Out of DATA
  1405. The computer says ``Out of DATA'' because it can't find ``pink'' in the DATA.     Avoid ``Out of DATA'' Instead of saying ``Out of DATA'', let's make the computer say ``I wasn't taught that color''. To do that, put an end mark at the end of the DATA; and when the computer reaches the end mark, make the computer say ``I wasn't taught that color'':
  1406. CLS
  1407. DATA white,blanc,yellow,jaune,orange,orange,red,rouge
  1408. DATA green,vert,blue,bleu,brown,brun,black,noir
  1409. DATA end,end
  1410. INPUT "Which color interests you"; request$
  1411. DO
  1412.         READ english$, french$
  1413.         IF english$ = "end" THEN PRINT "I wasn't taught that color": END
  1414.         IF english$ = request$ THEN EXIT DO
  1415. LOOP
  1416. PRINT "In French, it's "; french$
  1417.     In that program, the DO loop's purpose is to search through the DATA, to find DATA that matches the INPUT. Since the DO loop's purpose is to search, it's called a search loop.
  1418.     The typical search loop has these characteristics:
  1419. It starts with DO and ends with LOOP.
  1420. It says to READ a pair of data.
  1421. It includes an error trap saying what to do IF you reach the ``end'' of the data because no match found.
  1422. It says that IF you find a match (english$ = request$) THEN EXIT the DO loop.
  1423. Below the DO loop, say what to PRINT when the match is found.
  1424. Above the DO loop, put the DATA and tell the human to INPUT a search request.
  1425.     Auto rerun At the end of the program, let's make the computer automatically rerun the program and translate another color.
  1426.     To do that, make the bottom of the program say GO back TO the INPUT line:
  1427. CLS
  1428. DATA white,blanc,yellow,jaune,orange,orange,red,rouge
  1429. DATA green,vert,blue,bleu,brown,brun,black,noir
  1430. DATA end,end
  1431. 10 INPUT "Which color interests you"; request$
  1432. RESTORE
  1433. DO
  1434.         READ english$, french$
  1435.         IF english$ = "end" THEN PRINT "I wasn't taught that color": GOTO 10
  1436.         IF english$ = request$ THEN EXIT DO
  1437. LOOP
  1438. PRINT "In French, it's "; french$
  1439. GOTO 10
  1440.     The word RESTORE, which is above the search loop, makes sure that the computer's search through the DATA always starts at the DATA's beginning.
  1441.     Press Q to quit That program repeatedly asks ``Which color interests you'' until the human aborts the program (by pressing Ctrl with PAUSE/BREAK). But what if the human's a beginner who hasn't learned how to abort?
  1442.     Let's permit the human to stop the program more easily by pressing just the Q key to quit:
  1443. CLS
  1444. DATA white,blanc,yellow,jaune,orange,orange,red,rouge
  1445. DATA green,vert,blue,bleu,brown,brun,black,noir
  1446. DATA end,end
  1447. 10 INPUT "Which color interests you (press q to quit)"; request$
  1448. IF request$ = "q" THEN END
  1449. RESTORE
  1450. DO
  1451.         READ english$, french$
  1452.         IF english$ = "end" THEN PRINT "I wasn't taught that color": GOTO 10
  1453.         IF english$ = request$ THEN EXIT DO
  1454. LOOP
  1455. PRINT "In French, it's "; french$
  1456. GOTO 10
  1457.     END, STOP, or SYSTEM That program's shaded line ends by saying END. Instead of saying END, try saying STOP or SYSTEM.
  1458.     While the program is running, here's what the computer does when it encounters END, STOP, or SYSTEM:
  1459. STOP makes the program stop IMMEDIATELY. The screen becomes blue and shows the program's lines.
  1460.  
  1461. END makes the computer say ``Press any key to continue'' and wait for the human to press a key (such as F4 or ENTER). When the human finally presses a key, the screen becomes blue and shows the program's lines.
  1462.  
  1463. SYSTEM usually has the same effect as END. But if you saved the program onto the hard disk (using a name such as ``french.bas'') and then ran the program from DOS (by saying ``C:\>qbasic /run french''), SYSTEM makes the program stop IMMEDIATELY and makes the screen show ``C:\>''.
  1464.     A numeric constant is a simple number, such as:
  1465. 0     1     2     8     43.7     -524.6     .003
  1466. Another example of a numeric constant is 1.3E5, which means, ``take 1.3, and move its decimal point 5 places to the right''.
  1467.     A numeric constant does not contain any arithmetic. For example, since 7+1 contains arithmetic (+), it's not a numeric constant. 8 is a numeric constant, even though 7+1 isn't.
  1468.     A string constant is a simple string, in quotation marks:
  1469. "I love you"     "76 trombones"     "Go away!!!"     "xypw exr///746"
  1470.     A constant is a numeric constant or a string constant:
  1471. 0     8     -524.6     1.3E5     "I love you"     "xypw exr///746"
  1472.     A variable is something that stands for something else. If it stands for a string, it's called a string variable and ends with a dollar sign, like this:
  1473. a$     b$     y$     z$     my.job.before.promotion$
  1474. If the variable stands for a number, it's called a numeric variable and lacks a dollar sign, like this:
  1475. a      b      y      z      profit.before.promotion
  1476.     So all these are variables:
  1477. a$  b$  y$  z$  my.job.before.promotion$  a  b  y  z  profit.before.promotion
  1478.  
  1479.                                                                                            Expressions
  1480.     A numeric expression is a numeric constant (such as 8) or a numeric variable (such as b) or a combination of them, such as 8+z, or 8*a, or z*a, or 8*2, or 7+1, or even z*a-(7+z)/8+1.3E5*(-524.6+b).
  1481.     A string expression is a string constant (such as ``I love you'') or a string variable (such as a$) or a combination.
  1482.     An expression is a numeric expression or a string expression.
  1483.  
  1484.                                                                                            Statements
  1485.     At the end of a GOTO statement, the line number must be a numeric constant.
  1486. Right:    GOTO 100                            (100 is a numeric constant.)
  1487. Wrong:    GOTO n                                (n is not a numeric constant.)
  1488.     The INPUT statement's prompt must be a string constant.
  1489. Right:    INPUT "What is your name; n$    (``What is your name'' is a constant.)
  1490. Wrong:    INPUT q$; n$                        (q$ is not a constant.)
  1491.     In a DATA statement, you must have constants.
  1492. Right:    DATA 8, 1.3E5                    (8 and 1.3E5 are constants.)
  1493. Wrong:    DATA 7+1, 1.3E5                    (7+1 is not a constant.)
  1494. In the DATA statement, if the constant is a string, you can omit the quotation marks (unless the string contains a comma or a colon).
  1495. Right:        DATA "Joe","Mary"
  1496. Also right:    DATA Joe,Mary
  1497.     Here are the forms of popular BASIC statements:
  1498. General form                         Example
  1499. PRINT list of expressions            PRINT "Temperature is"; 4 + 25; "degrees"
  1500. LPRINT list of expressions           LPRINT "Temperature is"; 4 + 25; "degrees"
  1501. SLEEP numeric expression             SLEEP 3 + 1
  1502. GOTO line number or label            GOTO 10
  1503. variable = expression                x = 47 + 2
  1504. INPUT string constant; variable      INPUT "What is your name"; n$
  1505. IF condition THEN list of statements IF a >= 18 THEN PRINT "You": PRINT "vote"
  1506. SELECT CASE expression               SELECT CASE a + 1
  1507. DATA list of constants               DATA Joe,273,219,Mary,412,371
  1508. READ list of variables               READ n$, b, a
  1509. RESTORE line number or label         RESTORE 10
  1510. FOR numeric variable =               FOR I = 59 + 1 TO 100 + n STEP 2 + 3
  1511.     numeric expression TO
  1512.     numeric expression STEP
  1513.     numeric expression     Here's a strange program:
  1514. CLS
  1515. x = 9
  1516. x = 4 + x
  1517. PRINT x
  1518.     The third line (x = 4 + x) means: the new x is 4 plus the old x. So the new x is 4 + 9, which is 13. The bottom line prints:
  1519.  13
  1520.     Let's look at that program more closely. The second line (x = 9) puts 9 into box x:
  1521.        ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  1522. box x  ³        9    ³
  1523.        ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  1524. When the computer sees the next line (x = 4 + x), it examines the equation's right side and sees the 4 + x. Since x is 9, the 4 + x is 4 + 9, which is 13. So the line ``x = 4 + x'' means x = 13. The computer puts 13 into box x:
  1525.        ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  1526. box x  ³       13    ³
  1527.        ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  1528. The program's bottom line prints 13.
  1529.     Here's another weirdo:
  1530. CLS
  1531. b = 6
  1532. b = b + 1
  1533. PRINT b * 2
  1534. The third line (b = b + 1) says the new b is ``the old b plus 1''. So the new b is 6 + 1, which is 7. The bottom line prints:
  1535.  14
  1536.     In that program, the second line says b is 6; but the next line increases b, by adding 1 to b; so b becomes 7. Programmers say that b has been increased or incremented. In the third line, the ``1'' is called the increase or the increment.
  1537.     The opposite of ``increment'' is decrement:
  1538. CLS
  1539. j = 500
  1540. j = j - 1
  1541. PRINT j
  1542. The second line says j starts at 500; but the next line says the new j is ``the old j minus 1'', so the new j is 500 - 1, which is 499. The bottom line prints:
  1543.  499
  1544. In that program, j was decreased (or decremented). In the third line, the ``1'' is called the decrease (or decrement).                                                                                                                                                                                                                                 Counting
  1545.     Suppose you want the computer to count, starting at 3, like this:
  1546.  3
  1547.  4
  1548.  5
  1549.  6
  1550.  7
  1551.  8
  1552.  etc.
  1553. This program does it, by a special technique:
  1554. CLS
  1555. c = 3
  1556. DO
  1557.         PRINT c
  1558.         c = c + 1
  1559. LOOP
  1560.     In that program, c is called the counter, because it helps the computer count.
  1561.     The second line says c starts at 3. The PRINT line makes the computer print c, so the computer prints:
  1562.  3
  1563.     The next line (c = c + 1) increases c by adding 1 to it, so c becomes 4. The LOOP line sends the computer back to the PRINT line, which prints the new value of c:
  1564.  4
  1565.     Then the computer comes to the ``c = c + 1'' again, which increases c again, so c becomes 5. The LOOP line sends the computer back again to the PRINT line, which prints:
  1566.  5
  1567.     The program's an infinite loop: the computer will print 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, and so on, forever, unless you abort it.
  1568.     General procedure Here's the general procedure for making the computer count. . . . 
  1569.     Start c at some value (such as 3). Then write a DO loop. In the DO loop, make the computer use c (such as by saying PRINT c) and increase c (by saying c = c + 1).
  1570.     Variations To read the printing more easily, put a semicolon at the end of the PRINT statement:
  1571. CLS
  1572. c = 3
  1573. DO
  1574.         PRINT c;
  1575.         c = c + 1
  1576. LOOP
  1577. The semicolon makes the computer print horizontally:
  1578.  3  4  5  6  7  8  etc.
  1579.     This program makes the computer count, starting at 1:
  1580. CLS
  1581. c = 1
  1582. DO
  1583.         PRINT c;
  1584.         c = c + 1
  1585. LOOP
  1586. The computer will print 1, 2, 3, 4, etc.
  1587.     This program makes the computer count, starting at 0:
  1588. CLS
  1589. c = 0
  1590. DO
  1591.         PRINT c;
  1592.         c = c + 1
  1593. LOOP
  1594. The computer will print 0, 1, 2, 3, 4, etc.                                                                                               Quiz
  1595.     Let's make the computer give this quiz:
  1596. What's the capital of Nevada?
  1597. What's the chemical symbol for iron?
  1598. What word means `brother or sister'?
  1599. What was Beethoven's first name?
  1600. How many cups are in a quart?
  1601.     To make the computer score the quiz, we must tell it the correct answers:
  1602. Question                            Correct answer
  1603. What's the capital of Nevada?            Carson City
  1604. What's the chemical symbol for iron?    Fe
  1605. What word means `brother or sister'?    sibling
  1606. What was Beethoven's first name?        Ludwig
  1607. How many cups are in a quart?            4
  1608.     So feed the computer this DATA:
  1609. DATA What's the capital of Nevada,Carson City
  1610. DATA What's the chemical symbol for iron,Fe
  1611. DATA What word means 'brother or sister',sibling
  1612. DATA What was Beethoven's first name,Ludwig
  1613. DATA How many cups are in a quart,4
  1614.     In the DATA, each pair consists of a question and an answer. To make the computer READ the DATA, tell the computer to READ a question and an answer, repeatedly:
  1615. DO
  1616.         READ question$, answer$
  1617. LOOP
  1618.     Here's the complete program:
  1619. CLS
  1620. DATA What's the capital of Nevada,Carson City
  1621. DATA What's the chemical symbol for iron,Fe
  1622. DATA What word means 'brother or sister',sibling
  1623. DATA What was Beethoven's first name,Ludwig
  1624. DATA How many cups are in a quart,4
  1625. DATA end,end
  1626. DO
  1627.         READ question$, answer$: IF question$ = "end" THEN EXIT DO
  1628.         PRINT question$;                             
  1629.         INPUT "??"; response$                        
  1630.         IF response$ = answer$ THEN                  
  1631.                 PRINT "Correct!"                     
  1632.         ELSE                                         
  1633.                 PRINT "No, the answer is:  "; answer$
  1634.         END IF                                       
  1635. LOOP
  1636. PRINT "I hope you enjoyed the quiz!"
  1637.     The lines underneath READ make the computer PRINT the question, wait for the human to INPUT a response, and check IF the human's response matches the correct answer. Then the computer will either PRINT ``Correct!'' or PRINT ``No'' and reveal the correct answer. When the computer reaches the end of the DATA, the computer does an EXIT DO and prints ``I hope you enjoyed the quiz!''
  1638.     Here's a sample run, where I've underlined the parts typed by the human:
  1639. What's the capital of Nevada??? Las Vegas
  1640. No, the answer is:  Carson City
  1641. What's the chemical symbol for iron??? Fe
  1642. Correct!
  1643. What word means 'brother or sister'??? I give up
  1644. No, the answer is:  sibling
  1645. What was Beethoven's first name??? Ludvig
  1646. No, the answer is:  Ludwig
  1647. How many cups are in a quart??? 4
  1648. Correct!
  1649. I hope you enjoyed the quiz!
  1650.     To give a quiz about different topcs, change the DATA.     Count the correct answers Let's make the computer count how many questions the human answered correctly. To do that, we need a counter. As usual, let's call it c:
  1651. CLS
  1652. DATA What's the capital of Nevada,Carson City
  1653. DATA What's the chemical symbol for iron,Fe
  1654. DATA What word means 'brother or sister',sibling
  1655. DATA What was Beethoven's first name,Ludwig
  1656. DATA How many cups are in a quart,4
  1657. DATA end,end
  1658. c = 0
  1659. DO
  1660.         READ question$, answer$: IF question$ = "end" THEN EXIT DO
  1661.         PRINT question$;
  1662.         INPUT "??"; response$
  1663.         IF response$ = answer$ THEN
  1664.                 PRINT "Correct!"
  1665.                 c = c + 1
  1666.         ELSE
  1667.                 PRINT "No, the answer is:  "; answer$
  1668.         END IF
  1669. LOOP
  1670. PRINT "I hope you enjoyed the quiz!"
  1671. PRINT "You answered"; c; "of the questions correctly."
  1672.     At the beginning of the program, the human hasn't answered any questions correctly yet, so the counter begins at 0 (by saying ``c = 0''). Each time the human answers a question correctly, the computer does ``c = c + 1'', which increases the counter. The program's bottom line prints the counter, by printing a message such as:
  1673. You answered 2 of the questions correctly.
  1674.     It would be nicer to print ___ 
  1675. You answered 2 of the 5 questions correctly.
  1676. Your score is 40 %
  1677. or, if the quiz were changed to include 8 questions:
  1678. You answered 2 of the 8 questions correctly.
  1679. Your score is 25 %
  1680. To make the computer print such a message, we must make the computer count how many questions were asked. So we need another counter. Since we already used c to count the number of correct answers, let's use q to count the number of questions asked. Like c, q must start at 0; and we must increase q, by adding 1 each time another question is asked:
  1681. CLS
  1682. DATA What's the capital of Nevada,Carson City
  1683. DATA What's the chemical symbol for iron,Fe
  1684. DATA What word means 'brother or sister',sibling
  1685. DATA What was Beethoven's first name,Ludwig
  1686. DATA How many cups are in a quart,4
  1687. DATA end,end
  1688. q = 0
  1689. c = 0
  1690. DO
  1691.         READ question$, answer$: IF question$ = "end" THEN EXIT DO
  1692.         PRINT question$;
  1693.         q = q + 1
  1694.         INPUT "??"; response$
  1695.         IF response$ = answer$ THEN
  1696.                 PRINT "Correct!"
  1697.                 c = c + 1
  1698.         ELSE
  1699.                 PRINT "No, the answer is:  "; answer$
  1700.         END IF
  1701. LOOP
  1702. PRINT "I hope you enjoyed the quiz!"
  1703. PRINT "You answered"; c; "of the"; q; "questions correctly."
  1704. PRINT "Your score is"; c / q * 100; "%"
  1705.                                                                                              Summing
  1706.     Let's make the computer imitate an adding machine, so a run looks like this:
  1707. Now the sum is 0
  1708. What number do you want to add to the sum? 5
  1709. Now the sum is 5
  1710. What number do you want to add to the sum? 3
  1711. Now the sum is 8
  1712. What number do you want to add to the sum? 6.1
  1713. Now the sum is 14.1
  1714. What number do you want to add to the sum? -10
  1715. Now the sum is 4.1
  1716. etc.
  1717.     Here's the program:
  1718. CLS
  1719. s = 0
  1720. DO
  1721.         PRINT "Now the sum is"; s
  1722.         INPUT "What number do you want to add to the sum"; x
  1723.         s = s + x
  1724. LOOP
  1725.     The second line starts the sum at 0. The PRINT line prints the sum. The INPUT line asks the human what number to add to the sum; the human's number is called x. The next line (s = s + x) adds x to the sum, so the sum changes. The LOOP line sends the computer back to the PRINT line, which prints the new sum. The program's an infinite loop, which you must abort.
  1726.     General procedure Here's the general procedure for making the computer find a sum. . . . 
  1727.     Start s at 0. Then write a DO loop. In the DO loop, make the computer use s (such as by saying PRINT s) and increase s (by saying s = s + the number to be added).
  1728.  
  1729.                                                                                         Checking account
  1730.     If your bank's nasty, it charges you 20œ to process each good check that you write, and a $15 penalty for each check that bounces; and it pays no interest on the money you've deposited.
  1731.     This program makes the computer imitate such a bank. . . .
  1732. CLS
  1733. s = 0
  1734. DO
  1735.         PRINT "Your checking account contains"; s
  1736. 10      INPUT "Press d (to make a deposit) or c (to write a check)"; a$
  1737.         SELECT CASE a$
  1738.                 CASE "d"
  1739.                         INPUT "How much money do you want to deposit"; d
  1740.                         s = s + d
  1741.                 CASE "c"
  1742.                         INPUT "How much money do you want the check for"; c
  1743.                         c = c + .2
  1744.                         IF c <= s THEN
  1745.                                 PRINT "Okay"
  1746.                                 s = s - c
  1747.                         ELSE
  1748.                                 PRINT "That check bounced!"
  1749.                                 s = s - 15
  1750.                         END IF
  1751.                 CASE ELSE
  1752.                         PRINT "Please press d or c"
  1753.                         GOTO 10
  1754.         END SELECT
  1755. LOOP
  1756.     In that program, the total amount of money in the checking account is called the sum, s. The second line (s = 0) starts that sum at 0. The first PRINT line prints the sum. The next line asks the human to press ``d'' (to make a deposit) or ``c'' (to write a check).
  1757.     If the human presses ``d'' (to make a deposit), the computer asks ``How much money do you want to deposit?'' and waits for the human to type an amount to deposit. The computer adds that amount to the sum in the account (s = s + d).
  1758.     If the human presses ``c'' (to write a check), the computer asks ``How much money do you want the check for?'' and waits for the human to type the amount on the check. The computer adds the 20œ check-processing fee to that amount (c = c + .2). Then the computer reaches the line saying ``IF c <= s'', which checks whether the sum s in the account is big enough to cover the check (c). If c <= s, the computer says ``Okay'' and processes the check, by subtracting c from the sum in the account. If the check is too big, the computer says ``That check bounced!'' and decreases the sum in the account by the $15 penalty.
  1759.     How to program is nasty That program is nasty to customers. For example, suppose you have $1 in your account, and you try to write a check for 85œ. Since 85œ + the 20œ service charge = $1.05, which is more than you have in your account, your check will bounce, and you'll be penalized $5. That makes your balance will become negative $4, and the bank will demand that you pay the bank $4 ___ just because you wrote a check for 85œ!
  1760.     Another nuisance is when you leave town permanently and want to close your account. If your account contains $1, you can't get your dollar back! The most you can withdraw is 80œ, because 80œ + the 20œ service charge = $1.
  1761.     That nasty program makes customers hate the bank ___ and hate the computer!
  1762.     How to stop the nastiness The bank should make the program friendlier. Here's how.
  1763.     To stop accusing the customer of owing money, the bank should change any negative sum to 0, by inserting this line just under the word DO:
  1764.         IF s < 0 THEN s = 0
  1765.     Also, to be friendly, the bank should ignore the 20œ service charge when deciding whether a check will clear. So the bank should eliminate the line saying ``c = c + .2''. On the other hand, if the check does clear, the bank should impose the 20œ service charge afterwards, by changing the ``s = s - c'' to ``s = s - c - .2''.
  1766.     So if the bank is kind, it will make all those changes. But some banks complain that those changes are too kind! For example, if a customer whose account contains just 1œ writes a million-dollar check (which bounces), the new program charges him just 1œ for the bad check; $15 might be more reasonable.
  1767.     Moral: the hardest thing about programming is choosing your goal ___ deciding what you WANT the computer to do.                                                                      Series
  1768.     Let's make the computer add together all the numbers from 7 to 100, so that the computer finds the sum of this series: 7 + 8 + 9 + ... + 100. Here's how.
  1769.                                         CLS
  1770. Start the sum at 0:                        s = 0
  1771. Make i go from 7 to 100:                FOR i = 7 TO 100
  1772. Increase the sum, by adding each i to it:            s = s + i
  1773.                                         NEXT
  1774. Print the final sum (which is 5029):        PRINT s
  1775.     Let's make the computer add together the squares of all the numbers from 7 to 100, so that the computer finds the sum of this series: (7 squared) + (8 squared) + (9 squared) + . . . + (100 squared). Here's how:
  1776. CLS
  1777. s = 0
  1778. FOR i = 7 TO 100
  1779.         s = s + i * i
  1780. NEXT
  1781. PRINT s
  1782. It's the same as the previous program, except that indented line says to add i*i instead of i. The bottom line prints the final sum, which is 338259.
  1783.  
  1784.                                                                     Data sums
  1785.     This program adds together the numbers in the data:
  1786. CLS
  1787. DATA 5, 3, 6.1, etc.
  1788. DATA 0
  1789. s = 0
  1790. DO
  1791.         READ x: IF x = 0 THEN EXIT DO
  1792.         s = s + x
  1793. LOOP
  1794. PRINT s
  1795.     The DATA line contains the numbers to be added. The DATA 0 is an end mark. The line saying ``s = 0'' starts the sum at 0. The READ statement reads an x from the data. The next line (s = s + x) adds x to the sum. The LOOP line makes the computer repeat that procedure for every x. When the computer has read all the data and reaches the end mark (0), the x becomes 0; so the computer will EXIT DO and PRINT the final sum, s.