SDT Topic - 04
Upcoming SlideShare
Loading in...5
×
 

SDT Topic - 04

on

  • 691 views

 

Statistics

Views

Total Views
691
Views on SlideShare
378
Embed Views
313

Actions

Likes
0
Downloads
26
Comments
0

3 Embeds 313

http://www.pradipkharbuja.com.np 311
http://translate.googleusercontent.com 1
https://m.facebook.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

SDT Topic - 04 SDT Topic - 04 Presentation Transcript

  • 4 : ITERATION Er. Pradip Kharbuja
  • Before we start... 1. Write pseudocode to display "SDT" 10 times. 2. Write pseudocode to display "SDT" 1000 times.
  • 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
  • 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.
  • Iteration : Bounded Loop The bounded loop works based on a counter. 1. data counter as whole number 2. counter = 1 3. loop until counter is equal to 10 4. output "Software Development Techniques" 5. counter = counter + 1 6. next loop
  • Iteration : Bounded Loop ◦ Write pseudocode to display "SDT" 1000 times. 1. data counter as whole number 2. counter = 1 3. loop until counter is equal to 1000 4. output "Software Development Techniques" 5. counter = counter + 1 6. next loop
  • Equivalent Java Code : while loop 1. int counter; 2. counter = 1; 3. while ( counter != 1000) { 4. System.out.println(" Software Development Techniques "); 5. counter = counter + 1; 6. }
  • Equivalent Java Code : for loop 1. int counter; 2. for (counter = 1; counter != 1000; counter = counter + 1) 3. { 4. System.out.println(" Software Development Techniques "); 5. }
  • 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.
  • Iteration : Bounded Loop 1. data counter as whole number 2. counter = 1 // Intialisation 3. loop until counter is equal to 10 // Cotinuation Condition 4. output "Software Development Techniques" 5. counter = counter + 1 // Upkeep Condition 6. next loop
  • Iteration : Bounded Loop Exercise 1. Write pseudocode to display from 1 to 100. 2. Write pseudocode to display even numbers between 1 to 100. 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 in prime or not. 6. Write pseudocode to display following series (fibonacci series). 0 1 1 2 3 5 8 13 21
  • Sum 1 to 3 1. data sum as whole number 2. data counter as whole number 3. sum = 0 4. counter = 1 5. loop until counter is equal to 4 6. sum = sum + counter 7. counter = counter + 1 8. next loop 9. output sum
  • 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
  • Desk-Checking a Loop Line Num. sum counter Output Remarks 5 0 1 Repeat until counter is equal to 4 6 1 1 sum = sum + counter 7 1 2 Counter = counter + 1 8 1 2 Back to 5 Line Num. sum counter Output Remarks 5 1 2 Repeat until counter is equal to 4 6 3 2 sum = sum + counter 7 3 3 Counter = counter + 1 8 3 3 Back to 5
  • Desk-Checking a Loop Line Num. sum counter Output Remarks 5 3 3 Repeat until counter is equal to 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 until counter is equal to 4. Go to 9 9 6 4 6 Output
  • 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).
  • Sum 1 to 1000 1. data sum as whole number 2. data counter as whole number 3. loop until counter is equal to 1001 4. sum = sum + counter 5. counter = counter + 1 6. next loop 7. output sum
  • Desk-Check Line Sum Counter Notes 1 0 2 0 0 3 0 0 Repeat until counter (0) is equal to 1001 4 0 0 sum = sum + counter 5 0 1 counter = counter + 1 6 0 1 Back to 3 3 0 1 Repeat until counter (1) is equal to 1001 4 1 1 5 1 2 6 1 2 Back to 3 3 1 2 Repeat until counter (2) is equal to 1001
  • Desk-Check [Contd.] Line Sum Counter Notes 3 1 2 Repeat until counter (2) is equal to 1001 4 3 2 5 3 3 6 3 3 Back to 3 3 3 3 Repeat until counter (3) is equal to 1001 4 6 3 5 6 4 6 6 4 Back to 3 3 6 4 Repeat until counter (4) is equal to 1001 4 10 4 5 10 5 6 10 5 Back to 3
  • 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.
  • 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.
  • 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
  • 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
  • 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.
  • Condensing Loops Line Sum Counter Notes 1 0 2 0 0 3 0 0 Repeat until counter (0) is equal to 1001 4 0 0 sum = sum + counter 5 0 1 counter = counter + 1 6 0 1 Back to 3 3 0 1 Repeat until counter (1) is equal to 1001 4 1 1 5 1 2 6 1 2 Back to 3 3 1 2 Repeat until counter (2) is equal to 1001
  • Condensing Loops [Contd.] Line Sum Counter Notes 3 1 2 Repeat until counter (2) is equal to 1001 Condensed. counter = loop number + 1 sum = (loop number + counter) / 2
  • Condensing Loops [Contd.] Line Sum Counter Notes 3 498501 999 Repeat until counter (999) is equal to 1001 4 499500 999 5 499500 1000 counter = counter + 1 6 499500 1000 Back to 3 3 499500 1000 Repeat until counter (1000) is equal to 1001. 4 500500 1000 5 500500 1001 counter = counter + 1 6 500500 1001 Back to 3 3 500500 1001 Repeat until counter (1000) is equal to 1001. Go to 7 7 500500 1001 Output
  • 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.
  • 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.
  • 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
  • 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.
  • 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."
  • Unbounded Loop : Pseudocode ◦ Repeat until loop number is equal to user input 1. data i as whole number 2. data num as whole number 3. num = -1 4. i = 0 5. loop until num is equal to i 6. output "Please enter number" 7. input num 8. i = i + 1 9. next loop 10.output "No. times you entered value : " + i
  • 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.
  • 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
  • QUESTIONS ???
  • END OF TOPIC - 04