Class 2 Remote Instruction Introduction to Variables EDU

Class 2 Remote Instruction Introduction to Variables EDU 556 Programming for Instruction Dr. Steve Broskoske This is an audio Power. Cast. Make sure your volume is turned up, and press F 5 to begin. Click to advance as usual.

Introduction to Variables • On a calculator, it is many times helpful to store some information temporarily in a memory. • In programming, a “memory” is known as a variable. • A variable can store numeric information or a “string” (text).

Difference Between Calculator Memory and Variables Calculator Memory: • Most calculators have only 1 memory. • Memory actions: Variables: • Can have an unlimited number of variables. • Variable actions: – Add, clear, remember. • Can store only a number. • Can store integers (whole numbers), fractional numbers, text, true/false.

Common Variable Types Data Type Whole numbers (integers). Decimal numbers. “String” refers to text. “Boolean” refers to true/false. Description Range Integer 16 -bit integer -32768 to 32767 Long 32 -bit integer ± 2 billion (109) Single 16 -bit floating-point value ± 3. 4 x 1038 Double 32 -bit floating-point value ± 1. 8 x 10308 String characters/text up to 2 billion characters Boolean true or false, true Programmers select specific data types to increase storage efficiency and to restrict type of data stored.

Declaring a Variable • “Declaring” a variable means that you establish it in PC memory. • Variables can be declared in 3 different places, depending on how long the memory has to function: – Only needs to function within one event procedure. – Shared by more than one control on a form. – Needs to be shared by controls on more than the current form.

Where to Declare a Variable • Only needs to function within one event procedure. – Variable only needs to exist in the coding for one button. Declare the variable within the subroutine/procedure. • Needs to be shared by more than one control on a form. – Pressing one button will store info. in a variable, and pressing another button will recall the info. stored in the variable. • Needs to be shared by controls on more than the current form. – Info. is collected from the user on one form, and then processed on another form. Example is calculating a student’s quiz score from questions on several forms.

To Declare a Variable • Determine the type of data the variable will hold. – Integer (whole no. ) or single (decimal). – String (text). – Boolean (true or false). • Decide how the variable needs to function. – Within 1 event procedure/subroutine. – Within several event procedures on one form. – Over several forms. • Declare the variable.

To Declare a Variable Dim variable_name As data_type • • • Dim n As Integer Dim my. Number As Single Dim High. Num, Low. Num As Integer Dim my. Name As String Dim first. Name, last. Name As String Dim Answer As Boolean

To Declare a Variable Dim variable_name As data_type • Dim n As Integer, my. Name As String • Dim a, b, c, n As Integer, total As Long • Dim flag As Boolean Notice how many variables can be declared in 1 line of text in programming.

Storing a Value in a Variable variable_name = some information x = some value (in Algebra) • n=5 • my. Number = 20. 536 • my. Name = “Dr. Steve” • sentence = “This is my sentence. Or two. ” • flag = True

Concatenation • Notice that text, numbers, and true/false info. are all stored in different types of variables. • To combine these together, you must use the & (ampersand) character. • & (string concatenation character) allows you to combine strings (text) with other variable types (numbers or Boolean).

Concatentation • Declare several variables: – Dim small. Words, med. Words, long. Words As string • Set values for variables: – small. Words = “Hello. ” – med. Words = “My name is Lisa. ” • Blend (concatenate) strings: – long. Words = small. Words & med. Words • Variable long. Words now holds in memory: – Hello. My name is Lisa.

Concatenation • Declare several variables: – Dim age As Integer, maxcount As Integer, s As string • Set values for variables: – age = 5 maxcount = 100 s = “I am ” & age & “years old. I can count to ” & maxcount • Variable s now holds in memory: – I am 5 years old. I can count to 100.

Power of Variables Enter your name: Dr. Steve lbl. Instructions txt. Enter. Name Done Dim Student. Name As String Student. Name = txt. Enter. Name. text lbl. Show. Name. text = Student. Name lbl. Show. Name Dr. Steve

Next Class • Please review this material. If you enter our next class with a basic understanding of variables and their uses, this will save us about an hour of orientation. • At our next class, we will review these concepts and proceed directly to using them in coding.
- Slides: 15