The Basics of Programming in Visual BASIC
Enviado por mjarquin • 9 de Agosto de 2012 • 2.106 Palabras (9 Páginas) • 505 Visitas
The Basics of Programming in Visual BASIC
Computer programming is not difficult. It really amounts to
• Learning several general concepts
• Being careful, organised and logical
• Practice
In this primer, we will outline the general concepts using the Visual BASIC computer language.
There are only 8 fundamental concepts underlying computer programming to solve numerical problems confronted by engineers. These are
1. The “idea” of a program
2. Constants, variables, assignment and types
3. Mathematics
4. Decisions
5. Loops
6. Arrays
7. Macros, functions and subroutines
8. Input/output
The following sections provide some detail on each of these concepts.
1. THE “IDEA” OF A PROGRAM
A program is a set of instructions to tell a computer to do something. The simplest type is a sequence of instructions that the computer implements one after another in a mindless fashion. These instructions are sometimes called statements. For example, in Visual BASIC
Sub Adder()
a = 10
b = 33
c = a + b
Msgbox c
End Sub
Although some of the words might seems alien, it’s not too difficult to see that the computer will add two numbers together and then display the answer. In this case, because we’re using Excel, we employ a “message box” to display the answer on the spreadsheet.
The implication of a program as a series of instructions is that they must be unambiguous and totally logical. If not, the program won’t work the way you want it to. For example, suppose you redid the program and switched two of the statements
Sub Adder()
a = 10
b = 33
Msgbox c
c = a + b
End Sub
This wouldn’t give the desired result because it would try to display the answer before it had been calculated.
2. CONSTANTS, VARIABLES, ASSIGNMENT AND TYPES
Constants
There are three types of values (formally called constants) that are commonly used by engineers:
• Integers. That is, whole numbers like 86400 or -37.
• Real. That is, numbers with decimal points. These are also called floating point numbers in computer jargon. Examples would be 3.14159 and -9.81. Scientific notation is handled as in 1E-5 is equivalent to 1x10-5.
• Character. Alphanumeric information like names and labels such as “Sarah Jones” or “Oct. 1, 1998”
Variables and assignment
These are symbolic names that can be used to store constants. This is often accomplished with an assignment statement like
a = -32
b = 7.325E2
These would store a value of -32 in the variable a. Therefore, you can say a has a value of -32. Similarly, b has a value of 732.5 (i.e., 7.325x102).
It is useful to understand what the computer does when a line like a = 32 is executed. In essence, it sets up a location in its memory which it labels with an a and into which it stores the value of 32. The analogy of postal box locations is useful in this regard (Fig. 1).
FIGURE 1 Analogy between computer memory locations and postal boxes. Notice that different size boxes are used for each variable type.
Question: Which of the two following Visual BASIC statements is incorrect? Why?
x = x + 1
a + b = c + d
Types
The postal box analogy stresses the fact that different memory locations are required to store the 3 basic types of information. Some languages require that you declare the type of each variable. In VBA, variable “typing” is voluntary, but highly recommended. Because we believe that it’s excellent programming practice to type variables, we can force the issue by placing the following line at the beginning of a module,
Option Explicit
If we include such a line, we make typing mandatory. In such cases, all local variables (that is, those that do not originate elsewhere) must be “dimensioned” with a DIM statement. For example,
Dim a, b As Integer
Dim g As Single
Dim y as Double
Dim today As String
Dim x as Variant
Although you can “type” your variables in this fashion, Visual BASIC does not require that you do so.
For the foregoing Dim statements, you could not include the line
a = “October 1, 1998”
This would result in an error message, because you’re telling the computer to put a square peg in a round hole; that is, you’re telling it to store a string constant in a real, single precision memory location. If you do this, the program will not execute and you’ll get an error message.
3. MATHEMATICS
Mathematics are based on three key concepts: operator priority, left-to-right and parentheses.
Operator Priority
Visual BASIC follows the following priorities:
Therefore, if you had
x = 5 + 7 * 2
the computer would first multiply 7 * 2 to yield 14. It would then add this to 5 to give 19. The result would be assigned to the variable x.
Another example is
z = -2 ^ 2
The computer will first square the 2 to give 4 (since exponentiation supercedes negation). Then it will negate it to give the final answer: 4.
Left to Right
When choosing between two operations with equal priority, the computer implements them left-to-right. For example,
d = 10 / 5 * 7
It will first divide 10 by 5 to yield 2. Then it will multiply 2 * 7 to give the final answer 14. Notice how going right-to-left would give an entirely different answer:
10 / 5 * 7 10 / 35 0.285714285714286
Parentheses
The preceding rules can be overridden by using parentheses. That is the computer will evaluate operations in parentheses first and for nested parentheses will evaluate the innermost first. For example, suppose that we want to evaluate
If we write it as
y = 4 + 9 / 7
the computer will first divide 9 by 7 and then add the result to 4. To make it do the addition first, we enclose it in parentheses
y = (4 + 9) / 7
...