Top Banner
Looping & Concatenation LECTURE 4
71

Ma3696 lecture 4

Apr 22, 2015

Download

Education

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ma3696 lecture 4

Looping & Concatenation LECTURE 4

Page 2: Ma3696 lecture 4

Quick recap of variables For-Next loops Concatenation (&) Loops and Concatenation

SUMMARY

Page 3: Ma3696 lecture 4

QUICK RECAP

Page 4: Ma3696 lecture 4

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

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)

EXERCISE. REVIEW OF VARIABLES

Not necessarily in this order

Page 5: Ma3696 lecture 4

Declare disposable variables x, y and z as integer.

SOLUTIONS. REVIEW OF VARIABLES

Public Sub Recap() Dim x as integer, y as integer, z as integer End Sub

Page 6: Ma3696 lecture 4

Assign values to each variable.

SOLUTIONS. REVIEW OF VARIABLES

Public Sub Recap() Dim x as integer, y as integer, z as integer End Sub

x = 5 z = 10 y = x + z

x = 5 y = x + z z = 10

y = x + z x = 5 z = 10

Page 7: Ma3696 lecture 4

Assign values to each variable.

SOLUTIONS. REVIEW OF VARIABLES

Public Sub Recap() Dim x as integer, y as integer, z as integer x = 5 z = 10 y = x + z End Sub

Page 8: Ma3696 lecture 4

Output variable values to cells.

SOLUTIONS. REVIEW OF VARIABLES

Public Sub Recap() Dim x as integer, y as integer, z as integer x = 5 z = 10 y = x + z End Sub

Cells(1, 1) = x Cells(2, 1) = y Cells(3, 1) = z

x = Cells(1, 1) y = Cells(2, 1) z = Cells(3, 1)

Page 9: Ma3696 lecture 4

Output variable values to cells.

SOLUTIONS. REVIEW OF VARIABLES

Page 10: Ma3696 lecture 4

STEPPING THROUGH CODE

Page 11: Ma3696 lecture 4

Use the code you typed in the previous exercise

STEPPING THROUGH YOUR CODE

Page 12: Ma3696 lecture 4

Position screens so you can see both at once

STEPPING THROUGH YOUR CODE

Page 13: Ma3696 lecture 4

Click a start and end point for stepping through your code (it should turn red).

STEPPING THROUGH YOUR CODE

Click HERE

Click HERE

Page 14: Ma3696 lecture 4

Run your code (F5) It will pause at the first dot and turn yellow Use shift+F8 to run each line of code

STEPPING THROUGH YOUR CODE

Page 15: Ma3696 lecture 4

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.

HOVER OVER VARIABLES

Example: Check the value of y

Page 16: Ma3696 lecture 4

Watch the output to check it is correct Use this technique for checking For-Next loops!

CHECK THE OUTPUT

This code has not run yet

Page 17: Ma3696 lecture 4

FOR-NEXT LOOPS An Introduction

Page 18: Ma3696 lecture 4

Enter 1 to 5 into column 1 in Excel

EXAMPLE 1. FOR-NEXT LOOPS

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

Page 19: Ma3696 lecture 4

Use For-Next Loops to improve this code Identify what values are changing Identify what values are staying the same

EXAMPLE 1. FOR-NEXT LOOPS

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

Page 20: Ma3696 lecture 4

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

EXAMPLE 1. FOR-NEXT LOOPS

i = 1 i = 2 i = 3 i = 4 i = 5

Page 21: Ma3696 lecture 4

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

EXAMPLE 1. FOR-NEXT LOOPS

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

Page 22: Ma3696 lecture 4

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

EXAMPLE 1. FOR-NEXT LOOPS

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

Page 23: Ma3696 lecture 4

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

EXAMPLE 1. FOR-NEXT LOOPS

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

Page 24: Ma3696 lecture 4

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

EXAMPLE 1. FOR-NEXT LOOPS

i = 1 i = 2 i = 3 i = 4 i = 5

For every line of code the row is i

and the value is i

Page 25: Ma3696 lecture 4

i is an integer I need to declare

EXAMPLE 1. FOR-NEXT LOOPS

i = 1 i = 2 i = 3 i = 4 i = 5

Page 26: Ma3696 lecture 4

i is an integer I need to declare This is a For-Next Loop

EXAMPLE 1. FOR-NEXT LOOPS

i = 1 i = 2 i = 3 i = 4 i = 5

Page 27: Ma3696 lecture 4

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

EXAMPLE 1. FOR-NEXT LOOPS

i = 1 i = 2 i = 3 i = 4 i = 5

Page 28: Ma3696 lecture 4

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

EXAMPLE 1. FOR-NEXT LOOPS

i = 1 i = 2 i = 3 i = 4 i = 5

This code will run for each value of i

Page 29: Ma3696 lecture 4

FOR-NEXT LOOPS

Begin with FOR

End with NEXT

Page 30: Ma3696 lecture 4

A counter is a letter or word declared as a disposable integer variable

FOR-NEXT LOOPS

Must specify a COUNTER

Page 31: Ma3696 lecture 4

FOR-NEXT LOOPS

Values the COUNTER will take

Page 32: Ma3696 lecture 4

Increases by +1 by default END value must be > than START value

Can increment by -1 if necessary to count backwards

FOR-NEXT LOOPS

Must specify START value

Must specify END value

Page 33: Ma3696 lecture 4

Increases by +1 by default Can increment by -1 if necessary to count backwards

FOR-NEXT LOOPS

The result is the same for both loops

Page 34: Ma3696 lecture 4

Always START with 1 or 0! You will understand why when you start coding

FOR-NEXT LOOPS

Page 35: Ma3696 lecture 4

FOR-NEXT LOOPS

CODE to run goes inside the loop

Page 36: Ma3696 lecture 4

Use Lecture 4 Student Example.xlsm

Write a For-Loop for this: 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

EXERCISE 1. WRITE A FOR LOOP

Write this output to these cells

Run your code to ensure it works

Page 37: Ma3696 lecture 4

Enter 6 to 10 into column 2 in Excel

EXAMPLE 2. FOR-NEXT LOOPS

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

Page 38: Ma3696 lecture 4

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

EXAMPLE 2. FOR-NEXT LOOPS

i = 1 i = 2 i = 3 i = 4 i = 5

Page 39: Ma3696 lecture 4

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

EXAMPLE 2. FOR-NEXT LOOPS

When i = 1 row is 1

value is 6

When i = 1 row is i

value is i + 5

i = 1 i = 2 i = 3 i = 4 i = 5

Page 40: Ma3696 lecture 4

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

EXAMPLE 2. FOR-NEXT LOOPS

When i = 2 row is 2

value is 7

When i = 2 row is i

value is i + 5

i = 1 i = 2 i = 3 i = 4 i = 5

Page 41: Ma3696 lecture 4

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

EXAMPLE 2. FOR-NEXT LOOPS

When i = 3 row is 3

value is 8

When i = 3 row is i

value is i + 5

i = 1 i = 2 i = 3 i = 4 i = 5

Page 42: Ma3696 lecture 4

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

EXAMPLE 2. FOR-NEXT LOOPS

When i = 4 row is 4

value is 9

When i = 4 row is i

value is i + 5

i = 1 i = 2 i = 3 i = 4 i = 5

Page 43: Ma3696 lecture 4

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

EXAMPLE 2. FOR-NEXT LOOPS

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

Page 44: Ma3696 lecture 4

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

EXAMPLE 2. FOR-NEXT LOOPS

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

Page 45: Ma3696 lecture 4

Use Lecture 4 Student Example.xlsm

Write a For-Loop for this: 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

EXERCISE 2. WRITE A FOR LOOP

Write this output to these cells

(‘E’ is column 5)

Run your code to ensure it works

Page 46: Ma3696 lecture 4

Use 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

EXERCISE 3. WRITE A FOR LOOP

Write this output to these cells

Run your code to ensure it works

Page 47: Ma3696 lecture 4

CONCATENATION &

Page 48: Ma3696 lecture 4

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

CONCATENATE MEA N S TO JOIN TOGETHER

Page 49: Ma3696 lecture 4

EXAMPLES OF CONCATENATION

Not 6!

Page 50: Ma3696 lecture 4

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

Page 51: Ma3696 lecture 4

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

Page 52: Ma3696 lecture 4

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

Page 53: Ma3696 lecture 4

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

Page 54: Ma3696 lecture 4

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

Page 55: Ma3696 lecture 4

EXAMPLE 3. CONCATENATION

Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer)

End Sub

The text in “ ”

Page 56: Ma3696 lecture 4

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

Page 57: Ma3696 lecture 4

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 “ ”

Page 58: Ma3696 lecture 4

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…

Create a button in a worksheet to run this code.

EXERCISE 4. CONCATENATION

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.

Page 59: Ma3696 lecture 4

LOOPS WITH CONCATENATION

Page 60: Ma3696 lecture 4

Enter stock names in row 1 across columns 1-4

EXAMPLE 4. LOOPS WITH CONCATENTATION

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

Page 61: Ma3696 lecture 4

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

EXAMPLE 4. LOOPS WITH CONCATENTATION

i = 1 i = 2 i = 3 i = 4

Page 62: Ma3696 lecture 4

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

EXAMPLE 4. LOOPS WITH CONCATENTATION

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

Page 63: Ma3696 lecture 4

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

EXAMPLE 4. LOOPS WITH CONCATENTATION

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

Page 64: Ma3696 lecture 4

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

EXAMPLE 4. LOOPS WITH CONCATENTATION

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

Page 65: Ma3696 lecture 4

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

EXAMPLE 4. LOOPS WITH CONCATENTATION

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

Page 66: Ma3696 lecture 4

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

EXAMPLE 4. LOOPS WITH CONCATENTATION

For every line of code the column is i

and the stock is i

Page 67: Ma3696 lecture 4

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

EXERCISE 5. WRITE A FOR LOOP

Page 68: Ma3696 lecture 4

FINAL COMMENTS

Page 69: Ma3696 lecture 4

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

WHEN YOU’RE STUCK…

Page 70: Ma3696 lecture 4

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 for-

next loops to make code more efficient.

LEARNING OUTCOMES

Page 71: Ma3696 lecture 4

THE END