In programming, a constant is a specific value. Some values are numbers, and some are characters (called strings in VB).
Numeric constants can be divided into two groups: integers (whole numbers) and reals (numbers with decimal points). Some examples of integer constants are:
0 256 -99 6 16384 -7531
Some examples of real constants are:
0.05 3.14159 -15.25 99.99 8020.1
Notice that we don't put commas into a numeric constant.
A specific group of characters (a name, a word, a phrase, or any text) is called a string constant. A string constant can contain letters, digits, and/or special characters. Any character on the keyboard can be in a string constant. In VB, a string constant is always written inside of double quotes ("). Some examples of string constants are:
"http://YouTube.com" "Shadow of the Colossus" "hi" "R2D2"
In programming, a variable is a location in memory where a value can be stored. Variables are used to hold values that are read from forms, and values that are calculated during execution of a program. Every variable has a name and a type.
The name of a variable is used whenever we need to refer to the variable. There are rules for creating variable names:
The type of a variable determines what kind of data we can store in the variable. There are other types, but the basic types we will use are:
Type | Values | Examples |
---|---|---|
Integer | whole numbers from -32,768 to 32,767 | 389, -7, 0, 1024 |
Double | real numbers from -1.8 * 10308 to 1.8 * 10308, with about 14 digits of precision | 2.65, -.0005, 1139.25 |
String | characters | "last name", "hello!!" "P" |
Before you use a variable in an event procedure, you should declare it using the Dim statement. The form of the Dim statement is
Dim variable As type
For example,
Dim total As Double Dim temperature As Integer Dim name As String
When you declare a variable with a Dim statement, the variable is given an initial value. For numeric variables, the initial value is 0. For string variables, the initial value is the empty string (no characters) "". You can change the value of a variable with an assignment statement.
The assignment statement is used to store a value in a variable. The general form of the assignment statement is:
variable = expression
The left hand side of the assignment statement is a variable. The right hand side is an expression. An expression can be either a constant, a variable, or a calculation involving variables and/or constants. The effect of the assignment statement is to calculate the value of the right hand side, and then store that value in the variable on the left hand side.
For example, the following are all valid assignment statements that change the value of the variable on the left hand side.
count = 0 temperature = 47 num1 = num2 interest = balance * intrate name = "Jennifer Hudson" num2 = (num1 + 12) * 4 count = count + 1
In a program, the statements are executed in order from first to last. An assignment statement replaces the old value of a variable with a new value. The old value is lost. Here is an example of how the variables change as a program executes:
Statement | Variable Values After Execution | ||||
---|---|---|---|---|---|
num1 | num2 | i | count | phrase | |
Dim num1 As Double | 0 | ||||
Dim num2 As Double | 0 | 0 | |||
Dim i As Integer | 0 | 0 | 0 | ||
Dim count As Integer | 0 | 0 | 0 | 0 | |
Dim phrase As String | 0 | 0 | 0 | 0 | |
i = 9 | 0 | 0 | 9 | 0 | |
count = i | 0 | 0 | 9 | 9 | |
num1 = i / 2 | 4.5 | 0 | 9 | 9 | |
count = count + 1 | 4.5 | 0 | 9 | 10 | |
i = count * 2 | 4.5 | 0 | 20 | 10 | |
phrase = "Dragon Days" | 4.5 | 0 | 9 | 10 | Dragon Days |
num2 = (num1 + count) * 2 | 4.5 | 29.0 | 9 | 10 | Dragon Days |
The order of evaluation determines how to evaluate expressions with more than one operation. For example, if there is a multiplication and an addition, these rules tell us which operation is performed first. These rules should be familiar to you, because they are the same rules that we use in math:
For example, the expression 10 + 5 * 12 - 8 / 2 * 3 would be evaluated as follows:
10 + 5 * 12 - 8 / 2 * 3 10 + 60 - 8 / 2 * 3 10 + 60 - 4 * 3 10 + 60 - 12 70 - 12 58
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2007