Download presentation
Presentation is loading. Please wait.
Published byJob Hood Modified over 9 years ago
2
Class 2 Remote Instruction Introduction to Variables EDU 556 Programming for Instruction Dr. Steve Broskoske This is an audio PowerCast. Make sure your volume is turned up, and press F5 to begin. Click to advance as usual.
3
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).
4
Difference Between Calculator Memory and Variables Calculator Memory: Most calculators have only 1 memory. Memory actions: – Add, clear, remember. Can store only a number. Variables: Can have an unlimited number of variables. Variable actions: – Add, clear, remember. Can store integers (whole numbers), fractional numbers, text, true/false.
5
Common Variable Types Data TypeDescriptionRange Integer16-bit integer-32768 to 32767 Long32-bit integer±2 billion (10 9 ) Single16-bit floating-point value±3.4 x 10 38 Double32-bit floating-point value±1.8 x 10 308 Stringcharacters/textup to 2 billion characters Booleantrue or falsefalse, true Whole numbers (integers). Decimal numbers. “String” refers to text. “Boolean” refers to true/false. Programmers select specific data types to increase storage efficiency and to restrict type of data stored.
6
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.
7
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.
8
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.
9
To Declare a Variable Dim n As Integer Dim myNumber As Single Dim HighNum, LowNum As Integer Dim myName As String Dim firstName, lastName As String Dim Answer As Boolean Dim variable_name As data_type
10
To Declare a Variable Dim n As Integer, myName As String Dim a, b, c, n As Integer, total As Long Dim flag As Boolean Dim variable_name As data_type Notice how many variables can be declared in 1 line of text in programming.
11
Storing a Value in a Variable n = 5 myNumber = 20.536 myName = “Dr. Steve” sentence = “This is my sentence. Or two.” flag = True variable_name = some information x = some value (in Algebra)
12
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).
13
Concatentation Declare several variables: – Dim smallWords, medWords, longWords As string Set values for variables: – smallWords = “Hello.” – medWords = “My name is Lisa.” Blend (concatenate) strings: – longWords = smallWords & medWords Variable longWords now holds in memory: – Hello. My name is Lisa.
14
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.
15
Power of Variables Dr. Steve Enter your name: Dim StudentName As String StudentName = txtEnterName.text lblShowName.text = StudentName lblInstructions Dr. Steve lblShowName txtEnterName
16
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.