Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.

Similar presentations


Presentation on theme: "1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim."— Presentation transcript:

1 1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin

2 2 Syllabus Variables Variables Assignment Statement Assignment Statement NOT An Equation NOT An Equation Exchanging Values Exchanging Values Macro Exchange A1B1 Macro Exchange A1B1

3 3 Variables To represent values needed by a process, we’ll use variablesTo represent values needed by a process, we’ll use variables Variables refer to a location where a value can be stored, rather than a particular value; the value of a variable can change, hence the nameVariables refer to a location where a value can be stored, rather than a particular value; the value of a variable can change, hence the name A variable designates a location of a computer’s memoryA variable designates a location of a computer’s memory 3

4 4 Assignment Statement A VBA program is at times made up mostly of statementsA VBA program is at times made up mostly of statements The assignment statement assigns a value to a variableThe assignment statement assigns a value to a 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 5 NOT an Equation! In the statementIn the statement x = 4 the x refers to the memory location where the value 4 is to be stored, but after the assignment the two are indeed equalthe x refers to the memory location where the value 4 is to be stored, but after the assignment the two are indeed equal The meaning is, store the value 4 in the memory location designated by xThe meaning is, store the value 4 in the memory location designated by x Perhaps a more obvious syntax to document the assignment would be to write:Perhaps a more obvious syntax to document the assignment would be to write: x  4 But that is not how VBA defines assignmentsBut that is not how VBA defines assignments

6 6 Left Side, Right Side In the assignment statementIn the assignment statement x = x + 1 the x on the left refers to the memory location where a value will be storedthe x on the left refers to the memory location where a value will be stored VBA first evaluates the right side of the statement. It gets the current value from memory location x, adds 1 to it, and stores the result back in memory location xVBA first evaluates the right side of the statement. It gets the current value from memory location x, adds 1 to it, and stores the result back in memory location x The right hand side is evaluated first, and the computed result then moved to variable on the leftThe right hand side is evaluated first, and the computed result then moved to variable on the left

7 7 Order is Important! Consider the following sequence of statements: 1. x = 4location x now contains 4 2. y = 7location y now contains 7 3. x = x + ylocation x now contains 11 4. y = 3location y now contains 3 5. z = x + ylocation 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 changed y in line 4 Does NOT change the value of x as assigned in line 3

8 8 Exchanging Values of Variables Suppose I have variables x and y and I want to interchange their values. So if x = 4 and y = 5, I want to end up with x = 5 and y = 4. Here is what I might write at first: 1.x = y 2.y = x Line 1 assigns the value 5 to x. Now that x is 5, Line 2 assigns the value 5 to y The 4 was lost when line 1 was executed!

9 9 The Fix We need an extra variable to store one value while we exchange the other. Let’s call it temp: 1.temp = x 2.x = y 3.y = temp So now, temp gets the value 4 in line 1. Then x gets the value 5 in line 2, and y gets the value 4 in line 3

10 10 Excel Exchanges of Cell Values The workbook called ExchangeCellValues contains a macro called ExchangeA1B1 that exchanges the values of those two cells The workbook called ExchangeCellValues contains a macro called ExchangeA1B1 that exchanges 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 11 Macro ExchangeA1B1 '************************************************************* ' Exchange the values in Cell(1,1) and Cell(1,2) '************************************************************* Sub 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 temp As Variant Dim temp As Variant temp = Cells(1, 1).Value temp = Cells(1, 1).Value Cells(1, 1).Value = Cells(1, 2).Value Cells(1, 1).Value = Cells(1, 2).Value Cells(1, 2).Value = temp Cells(1, 2).Value = temp End Sub

12 12 The Cells(n,m) Notation Referring to cells by names like A1 and B1 is convenient for humans but not as good for programmingReferring to cells by names like A1 and B1 is convenient for humans but not as good for programming In programming we’ll use Cells(1,1) for A1, Cells(1,2) for B1, Cells(n,m) for row n, column mIn programming we’ll use Cells(1,1) for A1, Cells(1,2) for B1, Cells(n,m) for row n, column m Note the unfortunate fact that the Cells notation puts the row first and the column second, while the A1 notation does the reverse. Be careful to avoid errors with reversing the numbersNote the unfortunate fact that the Cells notation puts the row first and the column second, while the A1 notation does the reverse. Be careful to avoid errors with reversing the numbers

13 13 Demo: ExchangeA1B1 Run the ExchangeA1B1 macro. Don’t forget to enable macros when you open the ExchangeCellValues workbook.


Download ppt "1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim."

Similar presentations


Ads by Google