Download presentation
Presentation is loading. Please wait.
1
CHAPTER FOUR VARIABLES AND CONSTANTS
COMPUTER MATHEMATICS CHAPTER FOUR VARIABLES AND CONSTANTS
2
USING NAMED CONSTANTS Constant – memory location that stores a value that cannot be changed at run time from its initial value Use constants for unchanging values to make program easier to read Declared in a Const statement using identifier followed by the data type and an assignment
3
USING NAMED CONSTANTS Example: Const dblPi As Double = 3.14
This statement declares dblPi as data type Double and assigns the value of 3.14 to that memory location Constants should be declared before variables at the beginning of a procedure You cannot change the value of constant or an error will occur
4
CHOOSING IDENTIFIERS Must begin with a letter
Must contain only letters, digits, and the underscore (_) character Periods, spaces and other special characters are not allowed Cannot exceed 255 characters Cannot use keywords reserved by Visual Basic such as ‘Double’
5
CHOOSING IDENTIFIERS Appendix A has a list of keywords
Name identifiers so that their meaning is clear Prefixes describe the data type it will store Descriptive names are important for later modification or debugging
6
LAB TIME Review #3 Review #4
7
REVIEW #4 Examples: INPUT (reserved word) MY NAME (space)
23WESTCHESTER (begins with a number)
8
BUILT-IN DATA TYPES Variable or constant be of the appropriate data type Single – numbers possibly containing a decimal Double - numbers possibly containing a decimal Integer – integers (no decimals) Long – integers (no decimals) Currency – numbers representing dollar amount String – a set of characters Boolean – True or False
9
Single or Double Data Types
Positive or negative real numbers Referred to as floating point Difference between two types is the range of values it can store Single - can store values up to 3.4e38 Double – can store values up to 1.8e308
10
Integer and Long Data Types
Positive or negative integers No decimals Difference between two types is the range of values it can store Most times Integer is sufficient Integer range – 32,767 Long range – anything greater than 32,767 If a decimal value is assigned to an integer variable the value is rounded
11
CURRENCY, STRING, BOOLEAN DATA TYPES
Currency – can store values with up to four digits to the right of the decimal place and 15 digits to the left of the decimal place String – any set of characters enclosed in double quotation marks Boolean – True or False values only; good for representing Yes/No; Heads/Tails
12
VARIABLE PREFIXES Single – sgl Double - dbl Integer – int Long – lng
Currency – cur String – str Boolean - bln
13
VARIABLE DECLARATIONS
A single Dim statement can be used to declare several variables Example: Dim strSchool As String, intGrade as Integer Variables are initialized when declared; numeric values are zero, string variable blank (“”) and Boolean is False
14
OPTION EXPLICIT Can be added to the General section of the form module
Option Explicit displays error messages at run time when a variable is used before it is declared Select General from the Object list in the Code Editor window Type Option Explicit To make the Option Explicit statement automatically appear chose Options command from Tools menu then the Require Variable Declaration option from the Editor Tab
15
CLASSWORK Review #5
16
CLASSWORK ANSWER Dim strFirstName as String, strMiddleName as String
Dim strLastName as String, intAge as Integer Dim dblGPA as Double
17
Vocabulary Quiz Chapter Four Pg. 4-24
HOMEWORK Vocabulary Quiz Chapter Four Pg. 4-24
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.