# Chapter 7

Views:

Category: Entertainment

## Presentation Description

No description available.

## Presentation Transcript

### Foundations of Software Development N142:

Foundations of Software Development N142 Chapter 7

### Chapter 7: Iteration:

Chapter 7: Iteration Recursive functions are incredibly powerful, but they aren’t the only option for repetition options Iteration provides a number of options to help automate tasks

### Multiple Assignment:

Multiple Assignment Bruce=5 print ( B ruce) 5 Bruce=7 print(Bruce) 7 State Diagram:

### Multiple Assignment:

Multiple Assignment In this case Bruce was = 5 and then we assigned it to 7 Now imagine this example: a = 5 b = a #These variables are equal a = 3 #Now A is unique once again Note that A is unique in the above example, but it hasn’t changed the value of B

### Updating Variables:

Updating Variables x = x + 1 This gets the current value of x and adds one In order for this to work x must have had a prior value If not you’ll see this error: >>> x=x+1 Traceback (most recent call last): File "< stdin >", line 1, in <module> NameError : name 'x' is not defined

### Updating Variables:

Updating Variables The correct approach would be this: x =0 x=x+1 This is known as initialization and it’s required for the second statement to know what x equals When you add one to a variable it’s known as an increment , and subtracting by one is a decrement

### The while Statement:

The while Statement Remember countdown and print_n ? That used recursion to perform repetition, which is called iteration We can also perform iteration with a while statement That will continue to execute until it’s condition is met. Try this example: def countdown(n) while n > 0: print(n) n=n-1 print (‘Blastoff!’)

### The while Statement:

The while Statement Remember how countdown(1000) failed with the recursion example? Try it again now that it’s defined as a while statement Each iteration through it will evaluate n > 0 and when that is TRUE it will repeat the loop The one time it evaluates FALSE the while loop is broken

### Loops:

Loops When the flow of execution tells it to go back to the top and run it again then it’s defined as a loop The body of the loop should change one or more variables so that the condition becomes false and the loop terminates If the loop doesn’t terminate it’s known as an infinite loop

### Another Loop Example:

Another Loop Example def (sequence) while n != 1: print (n), if print n%2 == 0: # n is even n = n/2 else: #it’s odd n = n * 3 + 1 Continues while N is not equal to 1, so once n=1 the loop is done Each loop through checks for even or odd values Even = Divide by 2 Odd = Multiply by 3, add one

### Exercise 7.1 – print_n:

Exercise 7.1 – print_n Re-write print_n to use a while loop vs. recursion def print_n(s, n): if n <= 0: return print s print_n(s, n-1) >>> def print_n(s, n): ... while n!=0: ... print s ... n=n-1

### Break:

Break Have a loop that needs to be stopped? Let’s take a look at this infinite loop: booleanValue =True def infiniteloop ( someInt ): originalInt = someInt while booleanValue == True: print ( someInt ) someInt = someInt + 1 This would run forever. You could however stop this infiniteloop by calling break: if someInt == originalInt+1000: break

### Newton? Sq. Rt’s? Oy vey.:

Newton? Sq. Rt’s ? Oy vey. The math geek, err, author wanted to show the power of repetition so he picked square root calculations This formula will give you an approximation of the sq. rt. value, and it’ll get more accurate each time it is run a=4.0 x =3.0 y=( x+a /x) / 2 print y # would show 2.16666666667

### Newton? Sq. Rt’s? Oy vey.:

Newton? Sq. Rt’s ? Oy vey. Run once more: x=y y =(x+ a/x) / 2 Now we see 2.00641025641 so we’re even more accurate. Assign x = y and run the same thing x = y y=(x+ a/x) / 2 Then we get: 2.00001024003, and once more we see 2.00000000003 We don’t know how often it’ll take until we get the right answer, but once we get it the answer stops changing

### Exercise 7.2:

Exercise 7.2 Remember we used to test this example, make these inputs for a function: a=4.0 x=3.0 Automate it. Here is the key formula, give it a shot: y=(x+ a/x) / 2 >>> def squareit (a, x): ... while True: ... print (x) ... y = (x + a/x) / 2 ... if y == x: ... break ... x = y

### Exercise 7.2 – Lessons Learned:

Exercise 7.2 – Lessons Learned Can you always trust a float? 1/3 and √2 Store them as floats It’s not exact, what’s the solution? Function abs() Computes absolute values (magnitude of difference) Solution: if abs(y-x) < epsilon: break Epsiolon could be set to 0.0000001 for ex.

### Algorithms:

Algorithms Algorithms are a mechanical process for solving a category of problems Expressed another way… A step by step procedure for calculations

### Debugging:

Debugging Debugging becomes a much larger issue as the number of lines in your code increase Bisection Debugging is a method of breaking your code in to pieces and making assumptions based off that For example, inserting a print call on your code midway through it Verify the output matches the expected value You can then assume everything that executed prior to it is working properly

### Exercise 7.3 – Loop + Eval:

Exercise 7.3 – Loop + Eval Eval will run input through the Python interpreter eval (‘1 + 10 * 10’) = 101 Can you write a function eval_loop that will prompt the user for input, evaluate it, print the result, and continue to do that until they type ’ done’?

### Terms | Chapter Seven:

Terms | Chapter Seven Multiple Assignment Update Initialization Increment Decrement Iteration Infinite Loop