1 cs 106 computing fundamentals ii chapter 25 “variables, assignment statement” herbert g....
TRANSCRIPT
1
CS 106Computing Fundamentals II
Chapter 25“Variables, Assignment Statemen
t”Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CS
Status 7/4/2013Status 7/4/2013
Initial content copied verbatim fromInitial content copied verbatim fromCS 106 material developed byCS 106 material developed by
CS professors: Cynthia Brown & Robert MartinCS professors: Cynthia Brown & Robert Martin
2
Syllabus VariablesVariables Assignment StatementAssignment Statement NOT An EquationNOT An Equation Exchanging ValuesExchanging Values Macro Exchange A1B1Macro Exchange A1B1
3
Variables
• To represent values needed by a process, To represent values needed by a process, we’ll use variableswe’ll use variables
• VariablesVariables refer to a location where a refer to a location where a value can be stored, rather than a value can be stored, rather than a particular value; the value of a variable particular value; the value of a variable can change, hence the name can change, hence the name
• A variable designates a location of a A variable designates a location of a computer’s memorycomputer’s memory
3
4
Assignment Statement
• A VBA program is at times made up mostly of A VBA program is at times made up mostly of statementsstatements
• The assignment statement assigns a value to The assignment statement assigns a value to a variablea variable
• Examples:Examples:x = 7 ‘ assign 7 to x, type integer
name = “Cindy” ‘ assign the name Cindy to name, type string
x = x + 1 ‘ assign x 1 more than before, type integer
5
NOT an Equation!• In the statementIn the statement
x = 4
• the x refers to the memory location where the the x refers to the memory location where the value 4 is to be stored, but after the value 4 is to be stored, but after the assignment the two are indeed equalassignment the two are indeed equal
• The meaning is, store the value 4 in the The meaning is, store the value 4 in the memory location designated by xmemory location designated by x
• Perhaps a more obvious syntax to document the Perhaps a more obvious syntax to document the assignment would be to write:assignment would be to write:
x 4
• But that is not how VBA defines assignmentsBut that is not how VBA defines assignments
6
Left Side, Right Side
• In the assignment statementIn the assignment statement
x = x + 1x = x + 1
• the x on the left refers to the memory the x on the left refers to the memory location where a value will be storedlocation where a value will be stored
• VBA first evaluates the right side of the VBA first evaluates the right side of the statement. It gets the current value from statement. It gets the current value from memory location x, adds 1 to it, and stores memory location x, adds 1 to it, and stores the result back in memory location xthe result back in memory location x
• The right hand side is evaluated first, and The right hand side is evaluated first, and the computed result then moved to variable the computed result then moved to variable on the lefton the left
7
Order is Important!
Consider the following sequence of statements:Consider the following sequence of statements:1. x = 4 location x now contains 4
2. y = 7 location y now contains 7
3. x = x + y location x now contains 11
4. y = 3 location y now contains 3
5. z = x + y location z now contains 14
x is still 11 (no new assignment to x after receiving 11)
The value of expression x + y changed when we The value of expression x + y changed when we changed y in line 4changed y in line 4
Does NOT change the value of x as assigned in Does NOT change the value of x as assigned in line 3line 3
8
Exchanging Values of VariablesSuppose I have variables x and y and I Suppose I have variables x and y and I want to interchange their values. So if x want to interchange their values. So if x = 4 and y = 5, I want to end up with x = 5 = 4 and y = 5, I want to end up with x = 5 and y = 4. Here is what I might write at and y = 4. Here is what I might write at first:first:
1.1.x = yx = y
2.2.y = xy = x
Line 1 assigns the value 5 to x. Now that Line 1 assigns the value 5 to x. Now that x is 5, Line 2 assigns the value 5 to yx is 5, Line 2 assigns the value 5 to y
The 4 was lost when line 1 was executed!The 4 was lost when line 1 was executed!
9
The Fix
We need an extra variable to store one We need an extra variable to store one value while we exchange the other. Let’s value while we exchange the other. Let’s call it temp:call it temp:
1.1.temp = xtemp = x
2.2.x = yx = y
3.3.y = tempy = temp
So now, temp gets the value 4 in line 1. So now, temp gets the value 4 in line 1. Then x gets the value 5 in line 2, and y Then x gets the value 5 in line 2, and y gets the value 4 in line 3gets the value 4 in line 3
10
Excel Exchanges of Cell Values
The workbook called ExchangeCellValues The workbook called ExchangeCellValues contains a macro called ExchangeA1B1 that contains a macro called ExchangeA1B1 that exchanges the values of those two cellsexchanges the values of those two cells
The code is on the next slide. Note:The code is on the next slide. Note: Used a banner comment to describe what the macro does
Used comments within the code, too Indented lines to make everything more readable The VBA editor colored comments (green) and keywords (blue)
New way of referring to cells, Cells(n,m) Dim statement to declare variable temp
11
Macro ExchangeA1B1'*************************************************************'*************************************************************
' Exchange the values in Cell(1,1) and Cell(1,2)' Exchange the values in Cell(1,1) and Cell(1,2)
'*************************************************************'*************************************************************
SubSub ExchangeA1B1() ExchangeA1B1()
'Use a Variant data type so this works for any values in'Use a Variant data type so this works for any values in
'the two cells'the two cells
Dim Dim temp temp As VariantAs Variant
temp = Cells(1, 1).Valuetemp = Cells(1, 1).Value
Cells(1, 1).Value = Cells(1, 2).ValueCells(1, 1).Value = Cells(1, 2).Value
Cells(1, 2).Value = tempCells(1, 2).Value = temp
End SubEnd Sub
12
The Cells(n,m) Notation
• Referring to cells by names like A1 and B1 Referring to cells by names like A1 and B1 is convenient for humans but not as good is convenient for humans but not as good for programmingfor programming
• In programming we’ll use Cells(1,1) for In programming we’ll use Cells(1,1) for A1, Cells(1,2) for B1, Cells(n,m) for row A1, Cells(1,2) for B1, Cells(n,m) for row n, column mn, column m
• Note the unfortunate fact that the Cells Note the unfortunate fact that the Cells notation puts the row first and the column notation puts the row first and the column second, while the A1 notation does the second, while the A1 notation does the reverse. reverse. Be careful to avoid errors with Be careful to avoid errors with reversing the numbersreversing the numbers
13
Demo: ExchangeA1B1
Run the ExchangeA1B1 macro. Don’t Run the ExchangeA1B1 macro. Don’t forget to enable macros when you forget to enable macros when you
open the ExchangeCellValues open the ExchangeCellValues workbook.workbook.