|
|
|
|
When you program, it helps to be schizophrenic. Well, maybe not schizophrenic, but a little case of split personality is good. The programmer must take on two perspectives simultaneously. As you interact with VB, you are "the programmer". But you must always envision how the program will affect "the user". The user of the program may actually be you sometimes. In general, though, the user should be presumed to be someone else, someone who understands the program's goal but does not care about the internal workings of the program. Your task as a programmer is to make the program easy on the user, to prevent user errors and to anticipate what the user would expect or want. As you write the program, you must constantly think of how the user will react. The user will never know how hard you had to work to make the program easy to use. But you and I will know... When you write a program, how do you begin? Do you need to make a flow chart with cute boxes and arrows between them? Not. Certainly it helps to have at least a vague idea of what you want the program to accomplish, but you can make up the steps as you go along. You can insert elements anywhere in the program at any time. In your first programming efforts, you may feel some frustration as you realize it's taking you longer to accomplish your task with a computer than it would have with other means. Certainly you can add ten numbers faster with a calculator than even a skilled programmer can write and execute an addition program in VB. However, the computer with its program does have some real advantages over the calculator. The program needs to be written only once, and it can serve to handle any number of scores. The program can be easily modified to incorporate additional computations with the same data, such as finding the square of each score or combining values to compute a variance. For these additional computations, the scores would not have to be re-entered. The program never gets tired or forgets the formula. Another advantage is that the computer can easily be instructed to make a printed record of the scores and the results. Perhaps the most subtle advantage of the computer approach is that the program can monitor the input process, allowing the user to examine and correct the data without having to repeat the entire process should an error come to light. Incidentally, it is normal to make errors when constructing a program (even for very experienced programmers like me!). Don't get discouraged, just systematically fix them. You'll feel powerful. There are three major classes of errors. The dumb, annoying ones are typing errors. These are the most common errors, and some are easy to catch because VB responds to them with messages that suggest its inability to understand what you wrote. Other mistypings can be hard to find because they are not wrong from a stupid computer's perspective. For example, you may be operating with a quantity you have named MONEY. At some point in the program, you inadvertently call that same quantity MONDY. Of course, the computer has never heard of MONDY. Because nothing to the contrary has been articulated, the machine thinks MONDY has the default value of zero. (You can circumvent this problem to a considerable extent by using Option Explicit). Errors in logic are also hard to avoid, and they are also hard to catch because you may not be violating VB's syntax structure. You may have used the wrong operation, or have chosen the wrong option at a choice point in the program. Incorrect initialization is a common logic error. We'll look later at systematic ways to detect and debug. Initially you'll surely use what most programmers rely upon, the "stare" method. You stare at the code until you see an error. That does work fairly often, but the method tends to break down as programs grow in magnitude. Errors caused by ignorance lead to inefficient programs - they are slower to run, perhaps - but are not necessarily fatal to the program doing what it should. Increased knowledge helps, of course, but these errors tend to be the sort only professional programmers care about. You should not be concerned if someone tells you of a better way to solve your programming problem. If your way works, don't worry, be happy. Let's explore arithmetic operations in VB. A variable takes on an altered value by appearing in a replacement statement. A replacement statement looks like an equation, in that there is a variable name placed to the left of an equals sign. On the right of the equals sign, there may be other variable names, constants, and arithmetic operators. For example, A = B + 2 is a replacement statement. But you shouldn't think of the replacement statement as an equation although it may look like one. A = A + 2 is also a replacement statement, but not a soluble equation. The way to think of the meaning is that the program will replace the old value of A with the new value specified by the expression to the right of the equals sign. In fact, VB allows you to write the statement as LET A = A + 2, which makes the replacement concept more clear. But the extra typing is more than programmers can bear, so no one uses the redundant LET. To understand a program, you do just what the machine does. Trace through the steps mentally, keeping track of the value of each variable. The way to program is to be the computer. Sounds kind of Zen, doesn't it? Naturally the machine does a better job than you do, because it is faster, never forgets and carries out arithmetic flawlessly. Your advantage is that you know what it all means. The symbols for the arithmetic operations are not quite what you learned in grade school, although they are not difficult. Adding <+> and subtracting <-> do use the traditional symbols, but multiplication <*> and division </> are different. Raising to a power uses the caret <^>, located above the 6 on the keyboard. Parentheses work just as you expect them to. Let's try some examples. You figure out what values the series of A's should be. A1 = 5 + 3 A2 = A1 ^ 2 A3 = (A2 / A1) + 4 A4 = A1 + (A3 * 2) A1 = A4 - A2 A2 = A2 - A1 In this exercise, you are being the computer. First you are determining that A1 is 8. Then A2 is 64, A3 is 12, A4 is 32, and then A1 is reset to -32. Finally A2 is changed to 96. Note that the program always uses the current value of a variable without caring how that value came to be. For complex operations in which several operations are called for in a single expression, precedence rules are invoked. For example, how is this expression evaluated? A5 = A4 + A1/A2^2 The order of precedence is first exponentiation, then multiplication or division, then addition or subtraction. So here, A2 would first be raised to the second power, then A1 would be divided by that square, then A4 would be added. My personal preference is to use parentheses liberally so that I don't have to remember this rule. Excessive parentheses are not a violation of VB syntax. I would write A5 = A4 + A1/(A2^2) even though a professional programmer might scoff. It would even be OK to write A5 = A4 + (A1/(A2^2)) Just make sure the parenthesis pairs match up. |
|
|