A variable in programming refers to a block of memory reserved to hold specific data. A variable is so named because the programmer can change the data contained in the memory block as need permits. The variable is assigned a name by the programmer that is used any time the variable is referenced or manipulated. In most programming languages the variable is also designated a data type that helps the computer know what kind of data is being stored at the memory location and assign the correct amount of space to the variable. Purpose
The variable allows the programmer to store information temporarily within the program itself. The variable can contain information read from an external file, entered by the user or hard-coded by the programmer. Variables are often used to help the program make decisions and keep track of operations. For instance, a variable used as a counter can help a program keep track of how many times it has performed an operation or allow the program to perform mathematical operations and remember the outcome. http://www. ehow. com/info_8735864_variable-programming. html
•How do you declare and use variables in pseudocode? You declare variable in pseudo code by first making a list of the major tasks to be accomplished on a piece of paper scratch. Secondly, focus on the tasks and break each task down into smaller tasks, which can be explained in short phrases. •What are the types of variables, and what kinds of data can each type contain? Variables are placeholders used to store values; they have names and data types. The data type of a variable determines how the bits representing those values are stored in the computer’s memory.
When you declare a variable, you can also supply a data type for it. All variables have a data type that determines what kind of data they can store. By default, if you don’t supply a data type, the variable is given the Variant data type. The Variant data type is like a chameleon — it can represent many different data types in different situations. You don’t have to convert between these types of data when assigning them to a Variant variable: Visual Basic automatically performs any necessary conversion.
If you know that a variable will always store data of a particular type, however, Visual Basic can handle that data more efficiently if you declare a variable of that type. For example, a variable to store a person’s name is best represented as a string data type, because a name is always composed of characters. Data types apply to other things besides variables. When you assign a value to a property, that value has a data type; arguments to functions also have data types. In fact, just about anything in Visual Basic that involves data also involves data types.
You can also declare arrays of any of the fundamental types. For More Information For more information, see the section, “Arrays,” later in this chapter. Selecting data types to improve your application’s performance is discussed in “Designing for Performance and Compatibility. ” Declaring Variables with Data Types Before using a non-Variant variable, you must use the Private, Public, Dim or Static statement to declare it As type. For example, the following statements declare an Integer, Double, String, and Currency type, respectively: Private I As Integer Dim Amt As Double Static YourName As String
Public BillsPaid As Currency A Declaration statement can combine multiple declarations, as in these statements: Private I As Integer, Amt As Double Private YourName As String, BillsPaid As Currency Private Test, Amount, J As Integer Note If you do not supply a data type, the variable is given the default type. In the preceding example, the variables Test and Amount are of the Variant data type. This may surprise you if your experience with other programming languages leads you to expect all variables in the same declaration statement to have the same specified type (in this case, Integer).
Numeric Data Types Visual Basic supplies several numeric data types — Integer, Long (long integer), Single (single-precision floating point), Double (double-precision floating point), and Currency. Using a numeric data type generally uses less storage space than a variant. If you know that a variable will always store whole numbers (such as 12) rather than numbers with a fractional amount (such as 3. 57), declare it as an Integer or Long type. Operations are faster with integers, and these types consume less memory than other data types. They are especially useful as the counter variables in For…
Next loops. For More Information To read more about control structures, see “Introduction to Control Structures” later in this chapter. If the variable contains a fraction, declare it as a Single, Double, or Currency variable. The Currency data type supports up to four digits to the right of the decimal separator and fifteen digits to the left; it is an accurate fixed-point data type suitable for monetary calculations. Floating-point (Single and Double) numbers have much larger ranges than Currency, but can be subject to small rounding errors.
Note Floating-point values can be expressed as mmmEeee or mmmDeee, in which mmm is the mantissa and eee is the exponent (a power of 10). The highest positive value of a Single data type is 3. 402823E+38, or 3. 4 times 10 to the 38th power; the highest positive value of a Double data type is 1. 79769313486232D+308, or about 1. 8 times 10 to the 308th power. Using D to separate the mantissa and exponent in a numeric literal causes the value to be treated as a Double data type. Likewise, using E in the same fashion treats the value as a Single data type. The Byte Data Type
If the variable contains binary data, declare it as an array of the Byte data type. (Arrays are discussed in “Arrays” later in this chapter). Using Byte variables to store binary data preserves it during format conversions. When String variables are converted between ANSI and Unicode formats, any binary data in the variable is corrupted. Visual Basic may automatically convert between ANSI and Unicode when: •Reading from files •Writing to files •Calling DLLs •Calling methods and properties on objects All operators that work on integers work with the Byte data type except unary minus.
Since Byte is an unsigned type with the range 0-255, it cannot represent a negative number. So for unary minus, Visual Basic coerces the Byte to a signed integer first. All numeric variables can be assigned to each other and to variables of the Variant type. Visual Basic rounds off rather than truncates the fractional part of a floating-point number before assigning it to an integer. For More Information For details on Unicode and ANSI conversions, see “International Issues. ” The String Data Type If you have a variable that will always contain a string and never a numeric value, you can declare it to be of type String: Private S As String
You can then assign strings to this variable and manipulate it using string functions: S = “Database” S = Left(S, 4) By default, a string variable or argument is a variable-length string; the string grows or shrinks as you assign new data to it. You can also declare strings that have a fixed length. You specify a fixed-length string with this syntax: String * size For example, to declare a string that is always 50 characters long, use code like this: Dim EmpName As String * 50 If you assign a string of fewer than 50 characters, EmpName is padded with enough trailing spaces to total 50 characters.
If you assign a string that is too long for the fixed-length string, Visual Basic simply truncates the characters. Because fixed-length strings are padded with trailing spaces, you may find the Trim and RTrim functions, which remove the spaces, useful when working with them. Fixed-length strings in standard modules can be declared as Public or Private. In forms and class modules, fixed-length strings must be declared Private. For More Information See “Ltrim, RTrim Function and Trim Functions” in the Language Reference.