Okay, today we're going to start our "tutorial plot", or the characters that will carry us through the rest of the series. Namely, we're going to start a book store named the Cactus Flower. The Cactus Flower is run by a woman named Jane. Jane, unfortunately, can't afford any sort of new computer software as she has sunk the majority of her money into books and her business, so you've agreed to help her out. The first program we are going to make for her is a simple cash register, something to help her track sales (we'll assume that she doesn't need to store the records electronically right now). What will make this easier is that we are going to keep on improving this machine until it can read and write data from a disk, check for accurate prices, and keep running totals and averages. Nice, huh? This will be our main program for the rest of the tutorial. Pre-Programming: Part of programming at any time involves the process of pre-programming, or planning out your program beforehand. Taking our tutorial, when Jane wanted to start the Cactus Flower, she had to have a definite goal or end in minds. She couldn't just say, "I want to run a business and make lots of money," (a laudable goal, but a little too abstract). What sort of business? How much money? How much money will it cost to start up the business? Once she decided on a business, namely books, she then had to decide what books to stock, and for what cost. Starting a business isn't just about wanting the business, you have to plan the business. Same goes into programming. No program worth doing will be done in a single day, just like no tutorial will be done in a single day, or a single draft. What can be done, however, is to plan out the program into small, managable tasks. Doing this will reduce the chances of spaghetti code (more info later) and speed up debugging. Taking our, input-manipulation-output model, we find that originally it is too small for our ends. Look at it this way. Input - Input the number of items and the price Calculate-Total and sales tax (here, and for all models, we'll use 7%, or .075) Ouptut-Calculations, to the screen. You can easily see the problems here. We are assuming that there is _one_ price for all of the items: that is, the large novels cost the same as the comic books. Usually, that isn't the case (yes, Virginia, novels cost more than comics). We need to allow for more than one type of item, and allow people to have more than one of those items. However, the model isn't totally useless. Here's what we can expand it to. Ask for the price of the item. Ask for the number of items. Calculate out the subtotal. Add the subtotal to the total. Ask for another item. Ask for the number of items Calculate out the subtotal. Add the subtotal to the total. Ask for another item. This is better, but it poses another problem: how many times do we need to do this? We know that there is going to be more than one item, but we cannot accurately predict how many more than one item. This is where another powerful programming feature comes in, iteration. However, assuming that we do, we have to have a total plan of what we want to do. There are two ways to do this, flowcharts and pseudocode. Flowcharts and pseudocode have their relative advantages and disadvantages. Personally, I prefer pseudocode as it allows me to use my English skills, but in fairness to all sides, I'll demonstrate both. Pseudocode is probably the easiest way to pre-program, primarily because it requires no skills other than English. What you do is type what you want the program to do at each point. The example above was a demonstration of pseudocode, although not very good pseudocode. I would have written it this way. Ask for an item (price) Ask for the number of this item. Calculate out the subtotal Add the subtotal to the total Ask for another item, or 0 to quit. Loop until price = 0. The code states what I want it to do easily and quickly. In a verbal sense, it states what I want, a loop that runs until I enter a 0 price. The logic is obvious. What item costs "0" ? I could have said equal to or less than zero, but I have a reason for not doing that just yet, as we shall see. The other way to symbolize the code uses visual elements in a flowchart, like so: {Insert flowchart here} Look at what the flowchart does. In visual terms, it not only demonstrates the program and the individual commands, but the flow of the logic, which goes straight through then either drops out of the loop if the condition is met or returns through the loop if it is not. In later programs, you'll use flowcharts to break up lengthy code into simple, one-task subroutines that almost document themselves. Iteration: DO. . .LOOP and FOR. . .NEXT Iteration is a complex name for doing the same thing over and over. Humans are, by nature, lazy. They tend to go for the path which requires the least amount of energy unless strongly convinced otherwise. This isn't necessarily a bad thing. In fact, in several cases, it facilitates the invention of such technologies as the wheel, the cotton gin, and even the computer. Our problem is a perfect example. We don't want to uselessly spend energy typing in constant copies of the same thing when we don't even know whether we'll use all of them, or more. What we need is to tell the computer to do something over and over until a specific condition has been met. DO. . .LOOP does this task nicely. The syntax of this command is as follows. DO WHILE|UNTIL command command command LOOP WHILE|UNTIL A few things to note about this program. First thing you will notice is that WHILE|UNTIL is stuck on to the beginning and the end of this loop. This is because WHILE|UNTIL can be used either at the beginning of the problem, or the end of the problem. You shouldn't do it at both ends. Secondly, WHILE|UNTIL is a decision command you can use WHILE a condition is true or UNTIL a condition becomes true. Implementing this code for our calculations, we get this: INPUT "Input the price, 0 to quit: "; price DO UNTIL price = 0 INPUT "How many of this item? "; NumberOfItems Subtotal = price * NumberOfItems Total = Subtotal + Total INPUT "Input the price, 0 to quit: "; price LOOP No output comes out of this, but you can see what the program is doing. The first line is a primer input. When I start out my lawnmower, the only way I can start it up the first time is by pushing the primer to add some juice. With this loop, the same thing applies. when I first use the variable price, it's set to zero. So, if I haven't put anything into price, it will "drop out of the loop", or go to the first line that is after LOOP, in effect skipping the whole loop. There is another way to do this loop. INPUT "Input the price: "; price DO INPUT "How many of this item? "; NumberOfItems Subtotal = price * NumberOfItems Total = Subtotal + Total INPUT "Input the price, 0 to quit: "; price LOOP UNTIL price = 0 Still another way inserts the line LOOP WHILE price <> 0 In place of the final line. What it is now saying is "repeat the commands in the loop while price doesn't equal zero." All three possibilites do the same thing, with one notacable exception: The last example forces at least one input; the first example allows the user to "opt out". Here's another loop: let index = 0 DO UNTIL index > 10 print index index = index + 1 LOOP Which prints this: 0 1 2 3 4 5 6 7 8 9 10 Press any key to continue. . . Try it for yourself. In fact, try a few changes. One change that illustrates something not to do in programs. DO index = index + 1 PRINT index LOOP UNTIL index = 0 This is what is known as an _infinite loop_ meaning that it doesn't end. The first time the loop goes through, when it hits the "conditional" (UNTIL index=0), index is 1, so it loops again where it adds another. Obviously this loop isn't going to end any time soon. Try it. Type it in and run it (hit control + c to stop the program). FOR. . .TO. . .STEP. . .NEXT So far, we've looked at loops that do not have a predictable end (I don't want to use the word infinite). All of these loops only end if a certain condition is met, not "when". But suppose you know that a loop goes only a certain number of times. What then? Take the next to last code example: let index = 0 DO UNTIL index > 10 print index index = index + 1 LOOP We could write that loop like this: FOR index = 1 TO 10 PRINT index NEXT index FOR. . .TO. . .NEXT loops when you "know" will repeat a certain amount of times. They are often used to sort through lists, as well as do running totals. here's the general syntax of the loop: FOR variable = startingvalue to conditionalvalue step increment 'commands go here NEXT variable The FOR loop logic goes like this: 1. Check the value of the variable against the conditional. If the variable comparison matches the conditional, stop. If not, continue. 2. Do all of the statements between the FOR and the NEXT 3. Reach the next line and increment the variable by the increment. Here's an example factoral = 1 FOR index = 1 TO 5 factoral=factoral * index NEXT index print "The Factoral of 5 is "; factoral Assuming you typed everything correctly, you should get: The Factoral of 5 is 120 Press any key to continue. . . Note: Factorals are the product of the number and the integer values less than it, but greater than zero. The formula for a factoral is: n!=n * n-1 * n-2. . .* 1 For instance: 6! = 6 * 5 * 4 * 3 * 2 * 1 When you write a FOR loop, it automatically assumes that you want to increment the index number by one. You can overcome that by adding the STEP function. Example FOR index = 1 to 5000 STEP 10 NEXT index Will increment the variable index by tens. Likewise, it can also be decremented: FOR index = 6 to 1 STEP -1 Will decrease index. Be very careful. For instance, this: FOR index = 6 to 1 STEP 1 Will create an infinite loop. Also, never adjust the value of the variable just before FOR within the loop itself. For instance: FOR index = 1 to 4 index = 2 print index NEXT index creates an infinite loop. Don't catch it? This is one of the areas that a lot of programmers slip on. What this loop is doing is this: 1. Evaluates index against the conditional, which is 4, it isn't true, so it goes on. 2. Assigns the value of 2 to index 3. Prints index 4. Increments index 5. 3 is not 4, so go on. It will never break out of the loop because the program keeps assigning the index variable 2. Imagine trying to find that sort of error in 100 lines of code, or more. So, your best bet is not to alter the value of index within the loop. So, you may be looking back at one of the pieces of code and wondering if I'm saying two different things. Namely, I'm talking about the following: factoral = 1 FOR index = 1 TO 5 factoral=factoral * index NEXT index print "The Factoral of 5 is "; factoral The difference here is that index is not being altered here. The variable factoral is.