SDT Topic - 04

2,884 views

Published on

Published in: Education, Technology, Spiritual
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,884
On SlideShare
0
From Embeds
0
Number of Embeds
1,136
Actions
Shares
0
Downloads
134
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

SDT Topic - 04

  1. 1. Topic 4 : Iteration Software Development Techniques
  2. 2. Before we start... 1. Write pseudocode to display "SDT" 10 times. 2. Write pseudocode to display "SDT" 1000 times.
  3. 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 is also called as Loop. • It comes in two flavours. 1. Bounded iteration 2. Unbounded iteration
  4. 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. 5. 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.
  6. 6. 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. }
  7. 7. Equivalent Java Code : for loop 1. int counter; 2. for (counter = 0; counter < 10; counter = counter + 1) 3. { 4. System.out.println(" SDT"); 5. }
  8. 8. 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
  9. 9. 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.
  10. 10. 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
  11. 11. 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
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. 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
  16. 16. 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).
  17. 17. 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
  18. 18. 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.
  19. 19. 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.
  20. 20. 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
  21. 21. 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.
  22. 22. 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.
  23. 23. 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
  24. 24. 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
  25. 25. 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
  26. 26. 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.
  27. 27. 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
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. 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.
  32. 32. Unbounded Loop example • Write a pseudo code to take input until user enters 10.
  33. 33. Unbounded Loop example • Write a pseudocode to take input until user enters 10.
  34. 34. Unbounded Loop : Pseudocode • Repeat until loop number is equal to user input
  35. 35. Unbounded Loop : Pseudocode • Repeat until loop number is equal to user input
  36. 36. 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.
  37. 37. 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.
  38. 38. 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
  39. 39. Any Questions ??? End of Topic - 04

×