Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Development Techniques

Similar presentations


Presentation on theme: "Software Development Techniques"— Presentation transcript:

1 Software Development Techniques
Topic 4: Iteration NCC Education - Title Master

2 Scope of Topic In this topic we will discuss the computational term iteration. We use this to repeat blocks of an algorithm without having to repeat ourselves. Iteration opens up a lot of opportunities to make more interesting algorithms. In the past few weeks, we have been limited by what we can express in our pseudocode and desk-checks. Iteration requires some modifications to our pseudocode syntax and desk-checks.

3 Iteration Iteration is a different way of saying ‘repetition’.
It is used to partition off blocks of code so that they can be repeated. It comes in two flavours. Bounded iteration Unbounded iteration In this topic we will look at both why they are used, and how to represent them in our algorithms.

4 Why Iteration At the moment, you have the ability to repeat code within an algorithm. You just copy and paste it. There are several major problems with this: If you need to change the logic of one, you need to manually change the logic in all. If you do it enough times, the code becomes difficult to read and navigate. You need to manually count how many times you have repeated something.

5 Iteration in Pseudocode
To begin with, we will look at the simplest kind of loop. The bounded loop The bounded loop works based on a counter. Do this ten times To make use of a bounded loop, we need to set up a counter variable for it. Using our data keyword It will normally be called counter.

6 Iteration – Bounded Loop - 1
data counter as whole number data sum as whole number counter = 1 loop until counter is equal to 10 sum = sum + counter counter = counter + 1 Next Loop Note the indentation here – that is how we can tell what statements in our pseudocode belong to the loop.

7 Iteration – Bounded Loop - 2
Setting up a bounded loop has three parts to it: The initialisation, where the counter is given a starting value. We set that to one outside of the loop. The continuation condition This tells us when we stop looping. In this case, we stop looping when counter is ten. The upkeep condition This is done at the end of every loop, and we use it to ensure that the counter is incremented each time around.

8 Iteration – Bounded Loop - 3
data counter as whole number data sum as whole number counter = 1 // Initialisation Loop until counter is equal to 10 // Continuation sum = sum + counter counter = counter + 1 // Upkeep Next Loop Most programming languages will have a specific syntax for setting these up, but it cannot be assumed – thus, we handle all three parts manually in pseudocode.

9 Using a Loop What can we use loops for?
All sorts of things we could not do before! In our loops we do not have to use fixed numbers to indicate the continuation clause. We can also use the contents of the variables we have set up. For example: Repeat until counter is equal to user input This opens up lots of doors...

10 Bounded Loops Exercise
Consider the task of rising a user provided number to a user provided power. Could you do this before? You can do it now, using bounded loops. The user provides you with the base. The user provides you with the exponent. You use those two values to calculate the power of the first to the second.

11 Bounded Loops Answer We already thought through the logic for this in Topic 1. Now we can actually represent it in our formal pseudocode syntax. The process for this is: Set the base to be what the user provides Set the power to be what the user provides Set the total to the base Repeat a number of times equal to the power minus one Set total to be equal to total multiplied by the base

12 Pseudocode for Power 1 data base as whole number
2 data power as whole number 3 data total as whole number; 4 data counter as whole number 5 output "Please enter the base" 6 input base 7 output "Please enter the power" 8 input power 9 total = base; 10 counter = 1 11 loop until counter is equal to power 12 total = total * base 13 counter = counter + 1 14 next loop 15 Output total

13 Desk-Checking a Loop - 1 Iteration adds in a new complication for desk-checking. We need to loop backwards and forwards through the same set of code. We will talk about two strategies for managing this. The first is that for short loops that can be fully represented by hand, we simply introduce a new notation. When we reach the repeat line, we include the result of the condition.

14 Desk Checking a Loop - 2 Line Base Power Total Counter Notes 1 2 3 4 5
2 3 4 5 Output 6 User enters 5 7 8 User enters 3 9

15 Desk Checking a Loop - 3 Line Base Power Total Counter Notes 10 5 3 1 11 Repeat until counter (1) equals power (3) 12 25 13 2 14 Back to 11 In the notes for the repeat line, we include the value of each of the references variables. At the next loop line, we note which line of code we go back to (the one that holds the repeat).

16 Desk Checking A Loop - 4 Line Base Power Total Counter Notes 11 5 3 25 2 Repeat until counter (2) equals power (3) 12 125 13 14 Back to 11 Repeat until counter (3) equals power (3). Go to 15 15 Output We do this for each loop until the loop condition is met. At this point, we go to the next line of code after the next loop line.

17 Another Example Some loops are going to be long to do by hand.
In these cases, provided we can identify a pattern for how the loop impacts on our variables, we can condense it down. 1 data sum as whole number 2 data counter as whole number 3 Loop until counter is equal to 1000 4 sum = sum + counter 5 counter = counter + 1 6 next loop

18 Condensing a Desk-Check - 1
Line Sum Counter Notes 1 2 3 Repeat until counter (0) is equal to 1000 4 5 6 Back to 3 Repeat until counter (1) is equal to 1000 Repeat until counter (2) is equal to 1000

19 Condensing a Desk-Check - 2
Line Sum Counter Notes 3 1 2 Repeat until counter (2) is equal to 1000 4 5 6 Back to 3 Repeat until counter (3) is equal to 1000 Repeat until counter (4) is equal to 1000 10

20 Condensing a Desk-Check - 3
One of the powerful benefits of loops is that they are very flexible. If you want to do something a million times, you just put that in the loop. We work in the medium of pseudocode and desk-checks. That which is trivial for a computer, is not necessarily trivial for us. In these cases, provided we can identify a pattern, we can condense.

21 Finding a Pattern - 1 Programming is all about pattern matching.
If you can identify the patterns in information, you will find it much easier to design algorithms to manipulate them. Look at the way our variables were changing. If we can express that as some kind of formula, then we can tell, based on the number of loops, what the value is going to be.

22 Finding a Pattern - 2 Then, in our desk-check we would condense it.
Do the first couple Do the last couple

23 Find the Pattern - 3 Loop # Sum Counter 1 2 3 6 4 10 5 15 21 7 28 8 36 9 45 55 11 There is a pattern here between these numbers. If you find it, you can save yourself thousands of lines of work!

24 The Answer - 1 In the case of this pattern, we can get the value of sum by multiplying the loop number by the counter, and then halving it. The counter is always the loop number + 1 Do not worry if you didn’t see this – pattern matching is a skill that comes with practise. However, now that we know this, we know how to calculate the variables at any point in the loop. And that means we do not need to do every loop by hand.

25 The Answer - 2 Let us take loop 351.
Counter is loop + 1 352 Multiply the loop by the counter 123552 Halve it 61776 That is the value of sum at loop 351. How can we be sure of that? Well...

26 Condensing Loops The danger that comes with condensing a loop is that you miss out the error checking that comes with the loop. You need to be really, really sure that your pattern holds. Calculate enough loops that you can be sure that the pattern is reliable. This will vary from algorithm to algorithm. Then you can condense, like so:

27 Condense - 1 Condensed. Counter = loop number + 1
Line Sum Counter Notes 3 1 2 Repeat until counter (2) is equal to 1000 Condensed. Counter = loop number + 1 Sum = (loop number + counter) / 2

28 Condense - 2 Line Sum Counter Notes 3 497503 998
Repeat until counter (998) is equal to 1000 4 498501 5 999 6 Back to 3 Repeat until counter (999) is equal to 1000 499500 1000 Repeat until counter (1000) is equal to Go to 7

29 Condensing (Final) You should only do this for loops that are too big to desk-check by hand. Condensing means you can desk-check large loops without having to do it all manually, but it is still only an approximation. You cannot guarantee that something weird does not happen at loop 576, for example. When doing this, you should also have an actual answer you can check against. Otherwise, who knows?

30 Unbounded Loops - 1 The final topic for this lecture is the unbounded loop. This works exactly the same as the bounded loop, except it is not based on a counter. It is based on when some specified event is reached. For a bounded loop, you do not create a counter. And your condition becomes based on the value of some other variable.

31 Unbounded Loops - 2 Usually our conditions also look a little different: Instead of ‘is equal to’, we can also use one of: Is not equal to Is less than Is greater than Is less than or equal to Is greater than or equal to

32 Unbounded Loops - 3 These let you compare values in exactly that way.
We will talk a lot more about this in the next lecture. You can use unbounded loops to deal with uncertainty.

33 Bounded and Unbounded Loops
You use bounded loops when you know, at the time of running the desk-check, how many times you should loop. Repeat this ten times. You use unbounded loops when you do not know how many times you are going to repeat. Do this until the user presses the letter X. The two loops are used for slightly different things, but work in the same way.

34 Unbounded Loop example
1 data userInput as character 2 Loop until userInput is equal to Q 3 output "Give me a letter!" 4 input userInput 5 next loop 6 output "Oh, I really don't like that letter." We do not know when the user is going to enter a Q. Thus, we handle repeating until they do with an unbounded loop.

35 Conclusion In this lecture we looked at Bounded loops
When we know how many times we are going to repeat Unbounded loops When we do not know how many times we are going to repeat. Desk-check syntax Pattern matching and condensing of large loops

36 Terminology The following new pieces of terminology were introduced in this lecture: Condensing Removing the need to step through every line of code in a large loop by identifying the pattern that lets you approximate Bounded loop A loop that will iterate a known number of times Unbounded loop A loop that will iterate an unknown number of times

37 Topic 4 – Iteration Any Questions? NCC Education - End Slide Master


Download ppt "Software Development Techniques"

Similar presentations


Ads by Google