Operations on Values |
|
Values Fundamentals |
Introduction |
Besides the name, the second piece of information the compiler needs is the amount, also called size, of memory that the variable would need. This is because different values use different amounts of space. For example, the title of a book certainly needs more space than the number of pages of a daily newspaper. The amount of space that a variable can occupy is referred to as its data type. To limit the amount of gymnastics to perform, the compiler uses categories of data types. For example, it can decide that a simple number would use one small "box" to store its value. On the other hand, it may decide that it would need ten "buckets" to store the title of a movie. |
When you create a program, you can let the compiler know the amount of memory you would need for a particular variable. You can do this by specifying the most appropriate category. After telling the compiler that this particular variable would be used to store a number, you can change it in the middle of the program and decide to store an employee's maiden name. On the other hand, some numbers will require more space than others. For example, imagine you want to store the population of countries in a variable. In this case you would request memory that can hold a large number. This type of variable must be able to hold the population of China as well as the population of Burundi. Of course, this same space can hold the number of pages of a daily newspaper. This means that, sometimes, there will appear to be waste of memory. Nowadays, you should not be too concerned with memory as it is becoming less expensive: it is better to have a few empty "buckets" in the computer memory than to have unavailable memory. To reduce the number of mistakes that could be due to a possible wrong value being stored in a variable, the VBasic compiler uses two mechanisms: variable initialization and conversion.
We saw that, to declare a variable, you use the Dim keyword, followed by a name. Here is an example: Module Module1 Sub Main() Dim Something End Sub End Module After declaring a variable like this, the compiler reserves a portion of the computer memory for that variable but there may be two possible (small) problems: the compiler doesn't know how much space that variable would need to store its values and, because of this, the compiler would leave empty that area of memory. To access the value of a variable, you can simply refer to its name. For example, you can display it to the user. In Lesson 1, we saw that you could use the MsgBox procedure to display a value. Here is an example: Module Module1 Sub Main() Dim Something Something = 25 MsgBox(Something) End Sub End Module
We mentioned that you could declare a variable but not specify the type of value that would be stored in the memory area reserved for it. When you have declared a variable, the compiler gives it an initial value. This is referred to as initializing the variable. Instead of the compiler doing it, you too can initialize a variable. Initialization partially solves the two problems we mentioned. In reality, when you declare a variable, the compiler primarily considers it a string and reserves enough space to store any amount of characters. One way you can solve this confusion is to initialize the variable. To initialize a variable, type its name, followed by =, and followed by the desired value. Here is an example: Module Module1 Sub Main() Dim Something Something = 25 End Sub End Module After initializing the variable, the new value is stored in its reserved area of memory. When you initialize a variable, the compiler uses the given value to convert it to the appropriate type. For example, if you consider a variable with 25, it becomes considered an integral variable; that is, a variable whose memory can hold natural numbers. We mentioned that, after declaring a variable, you could change its value whenever you judge it necessary. After declaring a variable as done in the above example, you can assign it any value of you choice. Here are examples: Module Module1 Sub Main() Dim Something Something = 25 MsgBox(Something) Something = "Manchester United Football Club" MsgBox(Something) Something = 237565.408 MsgBox(Something) End Sub End Module This is one of the unique features that make the VBasic (2005) language so flexible.
While many programs are meant to simply present values to the user, most applications are used to request values from the user. To make this possible and easy, VBasic provides a procedure called InputBox. To use it, type it, followed by parentheses. In its parentheses, enter a sentence that will serve as a guide to the user. The sentence itself should be included between double-quotes. A typical sentence would indicate what value (or what type of value) you want the user to enter. Here is an example: InputBox("Enter your first name: ") The input box appears as a message box with the addition of a text box that would receive the user's value. Here is an example: The user is expected to type a value in the text box. As mentioned for the variables, the value the user types is primarily considered a regular series of characters. To use that series, you must convert it to the appropriate type. We will review the procedures used to convert the values.
An operation is an action performed on one or more values either to modify one value or to produce a new value by combining existing values. Therefore, an operation is performed using at least one symbol and one value. The symbol used in an operation is called an operator. A variable or a value involved in an operation is called an operand. A unary operator is an operator that performs its operation on only one operand. An operator is referred to as binary if it operates on two operands.
If you plan to write a long piece of code, to make it easier to read, you may need to divide it in various lines to do this, you can use the line continuation operator represented by an underscore. To do this, at the end of the line of code, put an empty space, then type the underscore, and continue your code in the next line. Here is an example: Module Module1 Sub _ Main() ' This line will not be considered as part of the code of VBasic") Rem I can write anything I want on this line End Sub End Module
Parentheses are used in two main circumstances: in a procedure as we have used Main, MsgBox and InputBox so far, or in an operation. The parentheses in an operation help to create sections in an operation. This regularly occurs when more than one operators are used in an operation. Consider the following operation: 8 + 3 * 5 The result of this operation depends on whether you want to add 8 to 3 then multiply the result by 5 or you want to multiply 3 by 5 and then add the result to 8. Parentheses allow you to specify which operation should be performed first in a multi-operator operation. In our example, if you want to add 8 to 3 first and use the result to multiply it by 5, you would write (8 + 3) * 5. This would produce 55. On the other hand, if you want to multiply 3 by 5 first then add the result to 8, you would write 8 + (3 * 5). This would produce 23. As you can see, results are different when parentheses are used on an operation that involves various operators. This concept is based on a theory called operator precedence. This theory manages which operation would execute before which one; but parentheses allow you to completely control the sequence of these operations.
The comma is used to separate variables used in a group. For example, a comma can be used to delimit the names of variables that are declared on the same line. Here is an example: Module Module1 Sub Main() Dim CustomerName, AccountNumber, InitialDeposit Dim RegularDeposit, TotalDeposits End Sub End Module The comma can also be used to separate the member of an enumerator or the arguments of a method. We will review all of them when the time comes. The assignment operation is used to make a copy of a value or the value of a variable and give the copy to another variable. The assignment operation is performed with the = sign. After you have declared a variable, before using it, it must have a value. One way you can give a value to a variable is to assign one.
A double-quote is used to delimit a group of characters and symbols. To specify this delimitation, the double-quote is always used in combination with another double-quote, as in "". What ever is inside the double-quotes is the thing that need to be delimited. The value inside the double-quotes is called a string. Here is an example: Sub Main() Dim Country Country = "Grande Bretagne" End Sub If a procedure expects a string, you can type that string in the parentheses of the procedure, we will be using the MsgBox and InputBox procedures.
Most of the time, to make various statements easier to read, you write each on its own line. VBasic allows you to write as many statements as necessary on the same line. To do this, the statements can be separated by a colon. Here is an example: Module Module1 Sub Main() Dim CustomerName Dim AccountNumber Dim InitialDeposit CustomerName = InputBox("Enter Customer Name:") AccountNumber = InputBox("Enter Customer Acnt #:") InitialDeposit = InputBox("Enter Initial Deposit:") MsgBox("Yugo National Bank") : MsgBox(CustomerName) MsgBox(AccountNumber) : MsgBox(InitialDeposit) End Sub End Module
The & operator is used to append two strings or expressions. This is considered as concatenating them. For example, it could allow you to concatenate a first name and a last name, producing a full name. The general syntax of the concatenation operator is: Value1 & Value2 To display a concatenated expression, use the assignment operator on the field. To assign a concatenated expression to a variable, use the assignment operator the same way: Sub Main() Dim FirstName Dim LastName Dim FullName FirstName = "Francis " LastName = "Pottelson" FullName = FirstName & LastName End Sub To concatenate more than two expressions, you can use as many & operators between any two strings or expressions as necessary. After concatenating the expressions or values, you can assign the result to another variable or expression using the assignment operator.
If you are displaying a string but judge it too long, you can segment it in appropriate sections as you see fit. To do this, you can use vbCrLf.
Algebra uses a type of ruler to classify numbers. This ruler has a middle position of zero. The numbers on the left side of the 0 are referred to as negative while the numbers on the right side of the rulers are considered positive:
A value on the right side of 0 is considered positive. To express that a number is positive, you can write a + sign on its left. Examples are +4, +228, +90335. In this case the + symbol is called a unary operator because it acts on only one operand. The positive unary operator, when used, must be positioned on the left side of its operand, never on the right side. As a mathematical convention, when a value is positive, you don't need to express it with the + operator. Just writing the number without any symbol signifies that the number is positive. Therefore, the numbers +4, +228, and +90335 can be, and are better, expressed as 4, 228, 90335. Because the value does not display a sign, it is referred as unsigned.
As you can see on the above ruler, in order to express any number on the left side of 0, it must be appended with a sign, namely the - symbol. Examples are -12, -448, -32706. A value accompanied by - is referred to as negative. The - sign must be typed on the left side of the number it is used to negate. Remember that if a number does not have a sign, it is considered positive. Therefore, whenever a number is negative, it MUST have a - sign. In the same way, if you want to change a value from positive to negative, you can just add a - sign to its left.
The addition is performed with the + sign. It is used to add one value to
another. Besides arithmetic operations, the + symbol can also be used to concatenate strings, that is, to add one string to another. This is done by appending one string at the end of another. Here is an example: Module Module1 Sub Main() Dim FirstName Dim LastName Dim FullName FirstName = "James " LastName = "Fame" FullName = FirstName + LastName MsgBox("Full Name: ") MsgBox(FullName) End Sub End Module
The multiplication operation allows you to add a number to itself a certain number of times set by another number. The multiplication operation is performed using the * sign. For example, to add 25 to itself 3 times, you would perform the operation as 25 * 3
The subtraction operation is performed using the - sign. This operation produces the difference of two or more numbers. It could also be used to display a number as a negative value. To subtract 28 from 65, you express this with 65-28. The subtraction can also be used to subtract the values of two values.
Dividing an item means cutting it in pieces or fractions of a set value. Therefore, the division is used to get the fraction of one number in terms of another. Microsoft Visual Basic provides two types of operations for the division. If you want the result of the operation to be a natural number, called an integer, use the backlash operator "\" as the divisor. The formula to use is: Value1 \ Value2 This operation can be performed on two types of valid numbers, with or without decimal parts. After the operation, the result would be a natural number.
The second type of division results in a decimal number. It is performed with the forward slash "/". Its formula is: Value1 / Value2 After the operation is performed, the result is a decimal number.
Exponentiation is the ability to raise a number to the power of another number. This operation is performed using the ^ operator (Shift + 6). It uses the following formula: yx In Microsoft Visual Basic, this formula is written as: y^x and means the same thing. Either or both y and x can be values, variables, or expressions, but they must carry valid values that can be evaluated. When the operation is performed, the value of y is raised to the power of x. The division operation gives a result of a number with or without decimal values, which is fine in some circumstances. Sometimes you will want to get the value remaining after a division renders a natural result. The remainder operation is performed with keyword Mod. Its formula is: Value1 Mod Value2 The result of the operation can be used as you see fit or you can display it in a control or be involved in another operation or expression. |
|
|
||
Previous | Copyright © 2004-2007 FunctionX, Inc. | Next |
|