sdt topic - 04

39
Topic 4 : Iteration Software Development Techniques

Upload: pradip-kharbuja

Post on 18-Jan-2015

1.056 views

Category:

Education


6 download

DESCRIPTION

 

TRANSCRIPT

Page 1: SDT Topic - 04

Topic 4 : IterationSoftware Development Techniques

Page 2: SDT Topic - 04

Before we start...

1. Write pseudocode to display "SDT" 10 times.

2. Write pseudocode to display "SDT" 1000 times.

Page 3: SDT Topic - 04

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 is also called as Loop.

• It comes in two flavours.

1. Bounded iteration

2. Unbounded iteration

Page 4: SDT Topic - 04

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.

Page 5: SDT Topic - 04

Iteration : Bounded Loop

The bounded loop works based on a counter.

1. data counter as whole number

2. counter = 0

3. loop while counter is less than 10

4. output "SDT"

5. counter = counter + 1

6. next loop

Write pseudocode to display "SDT" 1000 times.

Page 6: SDT Topic - 04

Equivalent Java Code : while loop

1. int counter;

2. counter = 0;

3. while ( counter < 10) {

4. System.out.println(" SDT");

5. counter = counter + 1;

6. }

Page 7: SDT Topic - 04

Equivalent Java Code : for loop

1. int counter;

2. for (counter = 0; counter < 10; counter = counter + 1)

3. {

4. System.out.println(" SDT");

5. }

Page 8: SDT Topic - 04

Iteration : Bounded Loop

1. data counter as whole number

2. counter = 0

3. loop while counter is less than 1000

4. output "SDT"

5. counter = counter + 1

6. next loop

Page 9: SDT Topic - 04

Iteration : Bounded Loop

• Setting up a bounded loop has three parts to it:1. The initialisation,

• the counter is given a starting value.

2. The continuation condition• This tells us when we stop looping

3. 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.

Page 10: SDT Topic - 04

Iteration : Bounded Loop

1. data counter as whole number

2. counter = 0 // Intialisation

3. loop while counter is less than 10 // Cotinuation Condition

4. output "Software Development Techniques"

5. counter = counter + 1 // Upkeep Condition

6. next loop

Page 11: SDT Topic - 04

Iteration : Bounded Loop Exercise

1. Write pseudocode to display from 1 to 100.

2. Write pseudocode to display even numbers between 1 to 100 inclusive.

3. Write pseudocode to find the sum of 1 to 10.

4. Write pseudocode to display result of 1 * 2 * 3 * 4 * 5 * 6 (factorial).

5. Write pseudocode to find whether the number input by user is prime or not.

6. Write pseudocode to display following series (fibonacci series).

0 1 1 2 3 5 8 13 21

Page 12: SDT Topic - 04

Sum 1 to 3

1. data sum as whole number

2. data counter as whole number

3. sum = 0

4. counter = 1

5. loop while counter is less than 4

6. sum = sum + counter

7. counter = counter + 1

8. next loop

9. output sum

Page 13: SDT Topic - 04

Desk-Checking a Loop

• Iteration adds in a new complication for desk-checking.

• We need to loop backwards and forwards through the same set of code.

Line Num. sum counter Output Remarks

1 0

2 0 0

3 0 0

4 0 1

Page 14: SDT Topic - 04

Desk-Checking a Loop

Line Num. sum counter Output Remarks

5 0 1 Repeat while counter is less than 4

6 1 1 sum = sum + counter

7 1 2 Counter = counter + 1

8 1 2 Back to 5

5 1 2 Repeat while counter is less than 4

6 3 2 sum = sum + counter

7 3 3 Counter = counter + 1

8 3 3 Back to 5

Page 15: SDT Topic - 04

Desk-Checking a Loop

Line Num. sum counter Output Remarks

5 3 3 Repeat while counter is less than 4

6 6 3 sum = sum + counter

7 6 4 Counter = counter + 1

8 6 4 Back to 5

Line Num. sum counter Output Remarks

5 6 4 Repeat while counter is less than 4

9 6 4 6 Output

Page 16: SDT Topic - 04

Desk-Checking a Loop

1. Write pseudocode to display even numbers between 1 to 5.

2. Write pseudocode to display result of 1 * 2 * 3 * 4 (factorial).

Page 17: SDT Topic - 04

Sum 1 to 1000

1. data sum as whole number

2. data counter as whole number

3. counter =1

4. sum = 0

5. loop while counter is less than 1001

6. sum = sum + counter

7. counter = counter + 1

8. next loop

9. output sum

Page 18: SDT Topic - 04

Condensing a Desk Check

• 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.

• 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.

Page 19: SDT Topic - 04

Finding a Pattern

• Programming is all about pattern matching.

• It is a skill that comes with practice.

• 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.

Page 20: SDT Topic - 04

Find a Patterns

0 1 2 3 4 5 6 7 8 9 10

1 3 5 7 9 11 13 15

0 2 4 6 8 10 12 14 16 18 20

0 1 1 2 3 5 8 13 21

2 3 5 7 11 13 17 19

1 4 9 16 25 36 49

1 8 27 64 125

1 4 27 256 3125

Page 21: SDT Topic - 04

Condensing Loops

• Condensing means you can desk-check large loops without having to do it all manually, but it is still only an approximation.

• It is not the same thing as doing and actual desk-check.

• You should only do this for loops that are too big to desk-check by hand.

Page 22: SDT Topic - 04

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.

Page 23: SDT Topic - 04

Desk-Check

Line Sum Counter Notes

1 0

2 0 0

3 0 0 Repeat while counter (0) is less than 1001

4 0 0 sum = sum + counter

5 0 1 counter = counter + 1

6 0 1 Back to 3

3 0 1 Repeat while counter (1) is less than 1001

4 1 1

5 1 2

6 1 2 Back to 3

3 1 2 Repeat while counter (2) is less than 1001

Page 24: SDT Topic - 04

Desk-Check [Contd.]

Line Sum Counter Notes

3 1 2 Repeat while counter (2) is less than 1001

4 3 2

5 3 3

6 3 3 Back to 3

3 3 3 Repeat while counter (3) is less than 1001

4 6 3

5 6 4

6 6 4 Back to 3

3 6 4 Repeat while counter (4) is less than 1001

4 10 4

5 10 5

6 10 5 Back to 3

Page 25: SDT Topic - 04

Find the Pattern

Loop Sum Counter

0 0 1

1 1 2

2 3 3

3 6 4

4 10 5

5 15 6

6 21 7

7 28 8

8 36 9

Page 26: SDT Topic - 04

Sum at loop 10

• Let us take loop 10.

• counter is loop + 1

• 11

• Multiply the loop by the counter

• 110

• Half it

• 55

• That is the value of sum at loop 10.

• What will be the value of sum at loop 1000.

Page 27: SDT Topic - 04

Condensing Loops

Line Sum Counter Notes

1 0

2 0 0

3 0 0 Repeat while counter (0) is less than1001

4 0 0 sum = sum + counter

5 0 1 counter = counter + 1

6 0 1 Back to 3

3 0 1 Repeat while counter (1) is less than 1001

4 1 1

5 1 2

6 1 2 Back to 3

3 1 2 Repeat while counter (2) is less than 1001

Page 28: SDT Topic - 04

Condensing Loops [Contd.]

Line Sum Counter Notes

3 1 2 Repeat while counter (2) is less than

1001

Condensed.

counter = loop number + 1

sum = (loop number * counter) / 2

Page 29: SDT Topic - 04

Condensing Loops [Contd.]

Line Sum Counter Notes

3 498501 999 Repeat while counter (999) is less than 1001

4 499500 999

5 499500 1000 counter = counter + 1

6 499500 1000 Back to 3

3 499500 1000 Repeat while counter (1000) is less than 1001.

4 500500 1000

5 500500 1001 counter = counter + 1

6 500500 1001 Back to 3

3 500500 1001 Repeat while counter (1001) is less than 1001. Go to 7

7 500500 1001 Output

Page 30: SDT Topic - 04

Some more conditions

• 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

Page 31: SDT Topic - 04

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 unbounded loop, you do not create a counter.

• We do not know in advance how many times loop is going to execute.

• You can use unbounded loops to deal with uncertainty.

Page 32: SDT Topic - 04

Unbounded Loop example

• Write a pseudo code to take input until user enters 10.

Page 33: SDT Topic - 04

Unbounded Loop example

• Write a pseudocode to take input until user enters 10.

Page 34: SDT Topic - 04

Unbounded Loop : Pseudocode

• Repeat until loop number is equal to user input

Page 35: SDT Topic - 04

Unbounded Loop : Pseudocode

• Repeat until loop number is equal to user input

Page 36: SDT Topic - 04

Write Pseudocode

• Take input from user i.e. greater than 10.

• Take input from user between 6 and 60 inclusive.

• Take input from user between -30 and 30 inclusive.

Page 37: SDT Topic - 04

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.

Page 38: SDT Topic - 04

Terminology

• Desk-check syntax

• Pattern matching and condensing of large loops

• 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

Page 39: SDT Topic - 04

Any Questions ???End of Topic - 04