variables hold information that may be manipulated, used to manipulate other information or...

13

Upload: kristian-simpson

Post on 08-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

Things you can do with Variables Copy and store values entered by the user Perform arithmetic manipulation on values Test values to see if they meet a criteria Temporarily hold and manipulate the value of a control property Remember information for later use in the progam

TRANSCRIPT

Page 1: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
Page 2: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

VariablesHold information that may be manipulated,

used to manipulate other information or remembered for later use

A storage location in memory (RAM)Holds information while the program is running

ContainsName (which cannot be changed)Data

Data type is declared and cannot be changed Value for the data can be changed during the program

Page 3: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

Things you can do with VariablesCopy and store values entered by the userPerform arithmetic manipulation on valuesTest values to see if they meet a criteriaTemporarily hold and manipulate the value of

a control propertyRemember information for later use in the

progam

Page 4: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

Declaring a VariableDeclaration statement creates the variable in

memoryIndicates the name to give the variable and

the type of information the variable will hold

DIM statementDim VariableName as DataType

Page 5: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

ExampleDim intLength as IntegerDim intLength as Integer = 5

Dim Stands for dimension statementTells VB that you are creating a variable

intLength the name to give the variableAs Integer indicates that the variable will

hold integer numbers= 5 assigning a beginning value to the

variable

Page 6: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

Variable Data TypesSee Handout

Boolean (bln)Byte (byt)Char (chr)Date (dtm)Decimal (dec)Double (dbl)Integer (int)Long (lng)

Object (default)(obj)

Short (shr)Single

(sng)String

(str)User Defined

Page 7: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

Variable NamesFirst character must be a letter or underscore

Follow 3 character prefix styleAfter first character – you may use letters,

number, underscoreCannot contain spaces or periodsUsing multiple words, capitalize the first letter

of each word (example: intTestScore)Cannot use VB KeywordsShould be meaningful

Page 8: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

Variable DeclarationsVariable should be declared first in the

procedure (style requirement)

Variable MUST be declared prior to the code where they are used

Declaring an initial value of the variable in the declaration statement is optionalWill default to a value of 0

Page 9: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

Scope of VariablesScope – the part of the program where the

variable can be usedVariable cannot be used before it is declaredVariables declared within a procedure / event

handling method is only visible to statements within that method Called Local Variables

Can be declared at the beginning of the code window (General Declarations section) and be available to all methods Called Form Level Variables

Cannot have more than one variable with the same cope with the same name

Page 10: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

Lifetime of VariablesLifetime – when the variable exists in

memoryA variables lifetime is the execution period of

the procedure/method that contains the variableWhen the procedure/method ends the local

variables are destroyed Memory is given back to the operating system

Page 11: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

Performing Calculations with VariablesArithmetic Operators

^ Exponential* Multiplication/ Floating Point Division\ Integer DivisionMOD Modulus (remainder from division)+ Addition Subtraction& String Concatenation (putting them

together)

Page 12: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

Mathematical Order of PrecedenceParenthesisExponentialMultiplication / DivisionInteger DivisionMODAddition / SubtractionString ConcatenationRelational Operators (< , > , >= , <= , <>)Logical Operators (AND, OR, NOT)

Page 13: Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)

Examples5 * (4 + 3) – 15 Mod 2

7 * 4 / 2 - 6