1 / 12

Data - Variables and Constants

Variables, Constants, and Computations. 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.

roch
Télécharger la présentation

Data - Variables and Constants

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables, Constants, and Computations 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

  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 NAME ADDRESS TYPE iCounter 00980909 integer stName 98100873 string When the program is compiled, each identifier is replaced by the corresponding address. Note: Memory can be thought of as a collection of cells iCounter 00980909 stName 98100873

  3. Data types Definition A data type is a set of values. Most important data types NAME OF TYPE VALUES Boolean True or False Integer Whole numbers in a given range (the range is machine dependent) Long Larger whole numbers Currency Numbers containing a decimal point Single Numbers containing a decimal point (6 digits of accuracy) Double Numbers containing a decimal point (14 digits of accuracy)

  4. NAME OF TYPE VALUES String Alphanumeric data (any visible symbol, such as letters, digits, punctuation marks, etc.) Variant Any type Note Long is allocated more space than integer. Doubleis allocated more space than single. Note 2 is of type Integer. 2.0 is of type Single (possibly Double), not integer. Example CONTENT TYPE REASON SSNo String No calculation Rate of pay Currency Decimal point Population Integer Whole number; used in calculation

  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. Constants- named and intrinsic - named constants (created by the programmer) - constant statement - general form Const Identifier As Type = value - Example Const TAXRATE As Single = 0.2 - intrinsic constants (built-in) Declaring variables - dim statement - general form Dim Identifier As Type - Example Dim sTax As Single

  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. - 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. 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. Counting and accumulating sums Example B. P. 83 miTotal = 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. Formatting data Defining string formats - general form Format$(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. 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

More Related