VARIABLES: Your Own Piece of the ram
Why Use Variables? To save a piece of data in memory To save a piece of data in memory To store results of a calculation To store results of a calculation To make code easier to modify/understand To make code easier to modify/understand
A variable is a named memory location that stores a value.
Variables must be declared before they can be used! DimIntlength As Integer Intlength - variable name Integer - data type Dim - stands for dimension As – reserved word
Variable Naming Rules Must begin with a letter. Must begin with a letter. Must only contain letters, digits, and underscores (_). Must only contain letters, digits, and underscores (_). Must not be a keyword (i.e. End, Sub, If). Must not be a keyword (i.e. End, Sub, If). Use the proper abbreviation Use the proper abbreviation (on the naming convention slide). (on the naming convention slide).
Naming Conventions Shortsrt Integerint Longlng Decimaldec Single sng Doubledbl Stringstr Charchr Datedte Booleanbln
Variable Data Types Numbers ( Whole Numbers or Decimals) Numbers ( Whole Numbers or Decimals) Whole Numbers Short, Integer, Long Decimal Single, Double,Decimal Text (String or Char) Text (String or Char) Dates (Date) Dates (Date) State (True or False: Boolean) State (True or False: Boolean)
How Do You Choose What Data Type to Use? Short -32,768 to 32,767 Integer-2,147,483,648 to 2,147,483,647 Longlarge integers Singlenumbers containing a decimal Doublelarge decimal numbers Date dates in m/d/yyyy format Decimalnumbers used for currency Chara single character Stringa set of characters in “ ” BooleanTrue or False
What Data Type Do You Use? 1. Number of students in class 2. Class average Choices 3. Name of the School 4. Grade Given as a Letter
What Kind of Variable Do You Use? 5. Distance to the Moon 6. Has the Bill Been Paid? Choices 7. Cost of a pizza 8. Date of the Last Day of School
Declaration Statements 1. Number of students in class 2. Class average 3. Name of the School 4. Grade Given as a Letter 5. Distance to the Moon 6. Has the Bill Been Paid? 7. Cost of a pizza 8. Date of the Last Day of School Dim srtstudent as short Dim srtstudent as short _______________________________ _________________________