Ma3696 lecture 4

293 views

Published on

Published in: Education, Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
293
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ma3696 lecture 4

  1. 1. LECTURE 4 Looping & Concatenation
  2. 2. SUMMARY Quick recap of variables For-Next loops Concatenation (&) Loops and Concatenation
  3. 3. QUICK RECAP
  4. 4. EXERCISE. REVIEW OF VARIABLES Download Lecture 4 Student Example.xlsm In Module1, find the procedure called Recap Declare disposable variables x, y and z as integer. Assign x a value of 5 Assign y a value of x + z Assign z a value of 10 Not necessarily in this order Output the value of x to cells(1, 1) Output the value of y to cells(2, 1) Output the value of z to cells (3, 1)
  5. 5. SOLUTIONS. REVIEW OF VARIABLES Declare disposable variables x, y and z as integer. Public Sub Recap() Dim x as integer, y as integer, z as integer End Sub
  6. 6. SOLUTIONS. REVIEW OF VARIABLES Assign values to each variable. Public Sub Recap() Dim x as integer, y as integer, z as integer x=5 y=x+z z = 10 End Sub x=5 z = 10 y=x+z y=x+z x=5 z = 10
  7. 7. SOLUTIONS. REVIEW OF VARIABLES Assign values to each variable. Public Sub Recap() Dim x as integer, y as integer, z as integer x=5 z = 10 y=x+z End Sub
  8. 8. SOLUTIONS. REVIEW OF VARIABLES Output variable values to cells. Public Sub Recap() Dim x as integer, y as integer, z as integer x=5 z = 10 y=x+z Cells(1, 1) = x Cells(2, 1) = y Cells(3, 1) = z End Sub x = Cells(1, 1) y = Cells(2, 1) z = Cells(3, 1)
  9. 9. SOLUTIONS. REVIEW OF VARIABLES Output variable values to cells.
  10. 10. STEPPING THROUGH CODE
  11. 11. STEPPING THROUGH YOUR CODE Use the code you typed in the previous exercise
  12. 12. STEPPING THROUGH YOUR CODE Position screens so you can see both at once
  13. 13. STEPPING THROUGH YOUR CODE Click a start and end point for stepping through your code (it should turn red). Click HERE Click HERE
  14. 14. STEPPING THROUGH YOUR CODE Run your code (F5) It will pause at the first dot and turn yellow Use shift+F8 to run each line of code
  15. 15. HOVER OVER VARIABLES Put your cursor over variables to see their current value. Variables will only show their value AFTER the line of code as run.  If the line is yellow, it means it hasn’t run yet, but it will run next. Example: Check the value of y
  16. 16. CHECK THE OUTPUT Watch the output to check it is correct Use this technique for checking For-Next loops! This code has not run yet
  17. 17. FOR-NEXT LOOPS An Introduction
  18. 18. EXAMPLE 1. FOR-NEXT LOOPS Enter 1 to 5 into column 1 in Excel Public Sub ForLoopExample1() Cells(1, 1).Value = 1 Cells(2, 1).Value = 2 Cells(3, 1).Value = 3 Cells(4, 1).Value = 4 Cells(5, 1).Value = 5 End Sub Very repetitive and inefficient
  19. 19. EXAMPLE 1. FOR-NEXT LOOPS Use For-Next Loops to improve this code  Identify what values are changing  Identify what values are staying the same Public Sub ForLoopExample1() Cells(1, 1).Value = 1 Cells(2, 1).Value = 2 Cells(3, 1).Value = 3 Cells(4, 1).Value = 4 Cells(5, 1).Value = 5 End Sub These values are changing
  20. 20. EXAMPLE 1. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5
  21. 21. EXAMPLE 1. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5 When i = 1 row is 1 value is 1 When i = 1 row is i value is i
  22. 22. EXAMPLE 1. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5 When i = 2 row is 2 value is 2 When i = 2 row is i value is i
  23. 23. EXAMPLE 1. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5 When i = 3 row is 3 value is 3 When i = 3 row is i value is i
  24. 24. EXAMPLE 1. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5 For every line of code the row is i and the value is i
  25. 25. EXAMPLE 1. FOR-NEXT LOOPS i is an integer I need to declare i=1 i=2 i=3 i=4 i=5
  26. 26. EXAMPLE 1. FOR-NEXT LOOPS i is an integer I need to declare This is a For-Next Loop i=1 i=2 i=3 i=4 i=5
  27. 27. EXAMPLE 1. FOR-NEXT LOOPS i is an integer I need to declare This is a For-Next Loop i is the counter – it will increase by 1 each time  The first value of i is 1  The last value of i is 5 i=1 i=2 i=3 i=4 i=5
  28. 28. EXAMPLE 1. FOR-NEXT LOOPS i is an integer I need to declare This is a For-Next Loop i is the counter – it will increase by 1 each time  The first value of i is 1  The last value of i is 5 This code will run for each value of i i=1 i=2 i=3 i=4 i=5
  29. 29. FOR-NEXT LOOPS Begin with FOR End with NEXT
  30. 30. FOR-NEXT LOOPS A counter  is a letter or word  declared as a disposable integer variable Must specify a COUNTER
  31. 31. FOR-NEXT LOOPS Values the COUNTER will take
  32. 32. FOR-NEXT LOOPS  Increases by +1 by default  END value must be > than START value  Can increment by -1 if necessary to count backwards Must specify START value Must specify END value
  33. 33. FOR-NEXT LOOPS  Increases by +1 by default  Can increment by -1 if necessary to count backwards The result is the same for both loops
  34. 34. FOR-NEXT LOOPS Always START with 1 or 0!  You will understand why when you start coding
  35. 35. FOR-NEXT LOOPS CODE to run goes inside the loop
  36. 36. EXERCISE 1. WRITE A FOR LOOP Use Lecture 4 Student Example.xlsm Write a For-Loop for this: Write this output to these cells Run your code to ensure it works  Remember to declare your counter!  Loop from 1 to 3 OR from 0 to 2  Write it out the long way first if you need to
  37. 37. EXAMPLE 2. FOR-NEXT LOOPS Enter 6 to 10 into column 2 in Excel Public Sub ForLoopExample2() Cells(1, 2).Value = 6 Cells(2, 2).Value = 7 Cells(3, 2).Value = 8 Cells(4, 2).Value = 9 Cells(5, 2).Value = 10 End Sub These values are changing
  38. 38. EXAMPLE 2. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5
  39. 39. EXAMPLE 2. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5 When i = 1 row is 1 value is 6 When i = 1 row is i value is i + 5
  40. 40. EXAMPLE 2. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5 When i = 2 row is 2 value is 7 When i = 2 row is i value is i + 5
  41. 41. EXAMPLE 2. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5 When i = 3 row is 3 value is 8 When i = 3 row is i value is i + 5
  42. 42. EXAMPLE 2. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5 When i = 4 row is 4 value is 9 When i = 4 row is i value is i + 5
  43. 43. EXAMPLE 2. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5 When i = 5 row is 5 value is 10 When i = 5 row is i value is i + 5
  44. 44. EXAMPLE 2. FOR-NEXT LOOPS Let i be a counter We have 5 lines of code, so  i will count from 1 to 5 Use the counter to change the row & output value i=1 i=2 i=3 i=4 i=5 For every line of code the row is i and the value is i + 5
  45. 45. EXERCISE 2. WRITE A FOR LOOP Use Lecture 4 Student Example.xlsm Write a For-Loop for this: Write this output to these cells (‘E’ is column 5) Run your code to ensure it works  Remember to declare your counter!  Count your loop from 1 to 5 OR from 0 to 4  Write it out the long way first if you need to
  46. 46. EXERCISE 3. WRITE A FOR LOOP Use Lecture 4 Student Example.xlsm Write a For-Loop for this: Write this output to these cells Run your code to ensure it works  Remember to declare your counter!  Count from 1 to 3 OR 0 to 2  Write it out the long way first if you need to
  47. 47. CONCATENATION &
  48. 48. CONCATENATE M EA N S TO JOIN TOGETHER Join together 2 or more…  String variables (i.e., text or words)  Text and variable values (integer, double or string)  Text and constant values  Variable values and constant values  Etc… Use the & to join two or more things together
  49. 49. EXAMPLES OF CONCATENATION Not 6!
  50. 50. EXAMPLE 3. CONCATENATION Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer) End Sub Declare a variable called answer
  51. 51. EXAMPLE 3. CONCATENATION Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer) End Sub Ask the user to enter a number Assign answer the number they enter
  52. 52. EXAMPLE 3. CONCATENATION Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer) End Sub Ask the user to enter a number Assign answer the number they enter
  53. 53. EXAMPLE 3. CONCATENATION Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer) End Sub Use a message box to tell the user what number they entered
  54. 54. EXAMPLE 3. CONCATENATION Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer) End Sub Use a message box to tell the user what number they entered
  55. 55. EXAMPLE 3. CONCATENATION Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer) End Sub The text in “ ”
  56. 56. EXAMPLE 3. CONCATENATION Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer) End Sub Use & to join text to a variable value
  57. 57. EXAMPLE 3. CONCATENATION Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer) End Sub The number entered by the user Variables DO NOT go in “ ”
  58. 58. EXERCISE 4. CONCATENATION Find the procedure for Exercise 4. Using 4 input boxes, obtain the following user info:  Their name  The name of the Uni they attend  The course they are studying  Their year at Uni (i.e. 1, 2, 3, …) Display their info in 1 MsgBox using full sentences.  e.g. “Your name is…”, “You attend…University”, etc… Tip: to display part of a message on a new line of a Msgbox, use & vbNewLine & where you want one line to end and another to begin. Create a button in a worksheet to run this code.
  59. 59. LOOPS WITH CONCATENATION
  60. 60. EXAMPLE 4. LOOPS WITH CONCATENTATION Enter stock names in row 1 across columns 1-4 Public Sub ForLoopExample4() Cells(1, 1).Value = “Stock1” Cells(1, 2).Value = “Stock2” Cells(1, 3).Value = “Stock3” Cells(1, 4).Value = “Stock4” End Sub These values are changing
  61. 61. EXAMPLE 4. LOOPS WITH CONCATENTATION Let i be a counter We have 4 lines of code, so  i will count from 1 to 4 Use the counter to change the col & output value i=1 i=2 i=3 i=4
  62. 62. EXAMPLE 4. LOOPS WITH CONCATENTATION Let i be a counter We have 4 lines of code, so  i will count from 1 to 4 Use the counter to change the col & output value i=1 i=2 i=3 i=4 When i = 1 column is 1 stock is 1 When i = 1 column is i stock is i
  63. 63. EXAMPLE 4. LOOPS WITH CONCATENTATION Let i be a counter We have 4 lines of code, so  i will count from 1 to 4 Use the counter to change the col & output value i=1 i=2 i=3 i=4 When i = 2 column is 2 stock is 2 When i = 2 column is i stock is i
  64. 64. EXAMPLE 4. LOOPS WITH CONCATENTATION Let i be a counter We have 4 lines of code, so  i will count from 1 to 4 Use the counter to change the col & output value i=1 i=2 i=3 i=4 When i = 3 column is 3 stock is 3 When i = 3 column is i stock is i
  65. 65. EXAMPLE 4. LOOPS WITH CONCATENTATION Let i be a counter We have 4 lines of code, so  i will count from 1 to 4 Use the counter to change the col & output value i=1 i=2 i=3 i=4 When i = 4 column is 4 stock is 4 When i = 4 column is i stock is i
  66. 66. EXAMPLE 4. LOOPS WITH CONCATENTATION Let i be a counter We have 4 lines of code, so  i will count from 1 to 4 Use the counter to change the col & output value For every line of code the column is i and the stock is i
  67. 67. EXERCISE 5. WRITE A FOR LOOP Using Lecture 4 Student Example.xlsm Write a For-Loop for this:  Remember to declare your counter!  Count from 1 to 3 OR 0 to 2  Write it out the long way first if you need to
  68. 68. FINAL COMMENTS
  69. 69. WHEN YOU’RE STUCK… When in doubt, write it out!!  If you aren’t sure how to write a loop, write it out without using loops so you can spot the pattern.  Then use a loop to make it more efficient. Step through your code to see what is happening during each loop.  Check variable values  Check output to cells/ranges You cannot loop variable names  E.g., “mean” & i  You have to use arrays (in a couple of weeks)  For now, those bits of code will have to be inefficient and long
  70. 70. LEARNING OUTCOMES You are ready to move on when…  LO17: You can name the 4 main parts of a For-Next loop.  LO18: Given the output you need, you can write a For-Next loop to achieve that output. Similarly, given a For-Next loop you can work out what the output/result will be (we will practice this in the lab). This is for one loop, not a nested loop (yet).  LO19: You can define the concept of concatenation and give some examples of how it is used in programming. You can use concatenation to join together values from text, variables and/or numbers. You also know when you need to use “ ” and when you should not use “ ”.  LO20: You can combine the use of concatenation with fornext loops to make code more efficient.
  71. 71. THE END

×