Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 P. 1 Data - Variables and Constants Definition A variable is a value which can change during program execution. A constant is a value which can.

Similar presentations


Presentation on theme: "Chapter 3 P. 1 Data - Variables and Constants Definition A variable is a value which can change during program execution. A constant is a value which can."— Presentation transcript:

1 Chapter 3 P. 1 Data - Variables and Constants Definition A variable is a value which can change during program execution. A constant is a value which can not change during program execution. An identifier is a name of a variable or constant. Variables are declared (named) in the declarations section of the program. Example Dim stName As String ‘Declare a string variable Dim iCounter as Integer ‘Declare an integer Const cDISCOUNT as Currency =.15 Variables, Constants, and Computations

2 Chapter 3 P. 2 Internal actions when declaring variables - memory location is reserved for the variable (symbol table is created) - value is placed into the location Example Dim iCounter As Integer Symbol table NAMEADDRESSTYPE iCounter00980909integer stName98100873string When the program is compiled, each identifier is replaced by the corresponding address. Note: Memory can be thought of as a collection of cells 00980909 98100873 iCounter stName

3 Chapter 3 P. 3 Data types Definition A data type is a set of values. Most important data types NAME OF TYPEVALUES BooleanTrue or False IntegerWhole numbers in a given range (the range is machine dependent) LongLarger whole numbers CurrencyNumbers containing a decimal point SingleNumbers containing a decimal point (6 digits of accuracy) DoubleNumbers containing a decimal point (14 digits of accuracy)

4 Chapter 3 P. 4 NAME OF TYPEVALUES StringAlphanumeric data (any visible symbol, such as letters, digits, punctuation marks, etc.) VariantAny type Note Long is allocated more space than integer. Double is allocated more space than single. Note2 is of type Integer. 2.0 is of type Single (possibly Double), not integer. ExampleCONTENTTYPEREASON SSNoStringNo calculation Rate of payCurrencyDecimal point PopulationIntegerWhole number; used in calculation

5 Chapter 3 P. 5 Naming rules - 1 to 40 characters - allowable symbols are letters, digits, and underline - no spaces are allowed - reserved words are not allowed (Print, Name, Caption, etc.) Naming conventions - identifiers must be meaningful (avoid 1-letter identifiers) - precede the identifier with a letter giving the type - capitalize variable names after the type - use all capitals for constants Example st = (inc - ft) * 0.2 is not as good as cState_Tax = (iIncome - cFederal_Tax)*0.2

6 Chapter 3 P. 6 Constants- named and intrinsic - named constants (created by the programmer) - constant statement - general formConst Identifier As Type = value - Example Const TAXRATE As Single = 0.2 - intrinsic constants (built-in) Declaring variables - dim statement - general formDim Identifier As Type - ExampleDim sTax As Single

7 Chapter 3 P. 7 Scope of variables Definition The scope of a variable is the code over which the variable is defined. Local declarations Definition A variable is local if its scope is one procedure. Example Sub cmd_Click() ‘Compute the tax Dim mcTax as Currency mcTax = iIncome * TAXRATE End Sub (Here iIncome and TAXRATE are declared at the module level.) Definition A variable is module-level if its scope is all the objects on the form. Example See the previous example.

8 Chapter 3 P. 8 - including the scope in identifiers (precede the name with m) - coding module-level declarations Calculations Arithmetic operators +-*/^ (exponent) - parentheses are also used - hierarchy is necessary because arithmetic expressions are written all on one line 1 Example ------- is written as 1/2 2 - hierarchy - exponent - multiply and divide - add and subtract Example 2 + 3 * 5 is computed as 2 + (3 * 5), which is 17

9 Chapter 3 P. 9 In general - expressions in parentheses are evaluated first, left to right - exponentiations are evaluated, left to right - multiplications and divisions are evaluated, left to right - additions and subtractions are evaluated, left to right Example B. P. 82 Using calculations in code General computations - general form is variable name = computation - Example lblTax.Caption = (iIncome -iDeductions * 3000) * 0.2 sDeterminant = sBcoeff^2 - 4 * sAcoeff *s Ccoeff Screen computations (center and resize form, if needed; B. P. 83) - frmMain.Top = (Screen.Height - frm.Height)/2 - frmMain.Left = (Screen.Width - frm.Width)/2 Note: This code goes in the Form_Load procedure

10 Chapter 3 P. 10 Counting and accumulating sums Example B. P. 83miTotal = miTotal + txtScore.text - get the value of miTotal - add the value of txtScore.text - place the result into miTotal Example ‘Increase the value of iCounter by 1. iCounter = iCounter + 1 Val function - changes a non-numeric value to a numeric value (for example, changes a blank to 0) - Example B.P. 84 iTotal = iTotal + val(txtScore.text) - the expression in parentheses is the argument

11 Chapter 3 P. 11 Formatting data Defining string formats - general formFormat$(expression, “FormatString”) - Example Format$(sName, “<“) ‘Write Name in lowercase letters - general format operators for strings B. P. 86 - space placeholder - null placeholder - left fill - Example B. P. 87 Defining numeric formats B. P. 87 - null placeholder - decimal point - thousand separator - percentage - literal character list

12 Chapter 3 P. 12 Programming hint Require variable declaration - use the Option Explicit feature (see the handout on Most frequently used Visual Basic commands) - select Tools - select Options - select Editor - check on Require variable declarations


Download ppt "Chapter 3 P. 1 Data - Variables and Constants Definition A variable is a value which can change during program execution. A constant is a value which can."

Similar presentations


Ads by Google