Iteration - PowerPoint PPT Presentation

About This Presentation
Title:

Iteration

Description:

Iteration. Repetition in computer ... Recursion vs Iteration 2. Every time a method is called, additional CPU time is incurred ... Recursion vs Iteration 2 ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 28
Provided by: University482
Learn more at: http://pirate.shu.edu
Category:
Tags: iteration

less

Transcript and Presenter's Notes

Title: Iteration


1
Iteration
  • Repetition in computer programming

2
Definition 1.28 Loop
  • A loop is a language structure that causes a
    block of code to repeat until certain
    conditions are met. A valid loop has three
    elements
  • The test a condition that determines whether a
    loop should continue or end.
  • The initialization the variable(s) used in the
    test must be initialized before the loop begins.

3
Tail recursion 2
  • Recall A recursive definition is one that
    defines the nth case of a concept in terms of the
    (n-1)st case plus some kind of boundary condition
  • Definition a method is tail recursive if all of
    its recursive calls occur as the last action
    performed in the method.

4
Loop (cont.) 1
  • The modification the variable(s) used in the
    test should be modified inside the loop.

5
printHello.java
  • public void printHello(int N)
  • if(Ngt1)
  • System.out.println(Hello)
  • printHello(N-1)
  • else
  • System.out.println(Hello)
  • Tail recursive algorithms can be converted to
    iterative algorithms fairly easily

6
Recursion to iteration
  • int k1
  • public void printHelloIterative(int N)
  • while (kltN)
  • System.out.println(Hello)
  • k

7
Tail Recursion 3
  • An interpreter which execute an iterative
    procedure in constant space is called tail
    recursive.
  • Tail recursion--a forerunner to the while loop
  • Compare the structure of 2 separate factorial
    programs

8
Recursion to iteration--alternate
  • public void printHelloIterative(int N)
  • while (Ngt0)
  • System.out.println(Hello)
  • N--

9
Recursive factorial
  • fact_iter(1, n)
  • fact_iter(n, n - 1)
  • fact_iter(n (n - 1), n - 2)
  • fact_iter(n (n - 1) (n - 2), n - 3)
  • .
  • .
  • .
  • fact_iter(n (n - 1) (n - 2) ... (n - (n -
    1)), 0)

10
Recursive factorial
  • ERROR IN NOTES At the counter th stage of the
    computation, the first
  • parameter fprod has value
  • n (n - 1) ... (n - (counter 1))
  • Should be
  • n (n - 1) ... (counter 1)

11
(But ... is dangerous what about the very
first step?)
  • static int fact (int n)
  • return fact_iter(1,n)
  • static int fact_iter(int fprod, int counter)
  • if (counter 0)
  • return (fprod)
  • else
  • return fact_iter(counter fprod, counter - 1)

12
Proposed
  • pre counter gt 0
  • post returns fprod counter!
  • Invariant relation
  • fprod counter! n!
  • fprod counter fprod counter!
  • 1 4 1 4! 4!
  • 4 3 (4) 3! 4!
  • 12 2 (4 3) 2! 4!
  • 24 1 (4 3 2) 1! 4!
  • 24 0 (4 3 2 1) 1 4!

13
static int fact (int n)
  • f // n gt 0
  • return fact_iter(1,n) // n! is returned
  • g static int fact_iter(int fprod, int counter)
  • f //invariant fprod counter! n! where n is
    the argument to fact
  • if (counter 0)
  • return (fprod)
  • // fprod counter! n! counter 0, so...
  • else
  • return fact_iter((counter fprod), (counter -
    1))
  • //invariant preserved frpod counter! n!

14
Invariant 3
  • public static int iterfact(int current,int
    counter)
    //invariant currentcounter!n!, n
    //argument of fact
  • if (counter0 ) return (current)
  • //currentcounter!n!counter0,so
    //currentn!
  • else return iterfact countercurrent,counter-1)

15
Invariant
  • For any tail recursive function, there exists a
    relation among its parameters that never changes
  • Here, currentcounter! n!
  • public static int iterfact(int current,int
    counter)
  • if (counter0 ) return (current)
  • else return iterfact(counter
    current,counter-1)

16
Recursion vs Iteration 2
  • Every time a method is called, additional CPU
    time is incurred
  • Each time a method is called, Java must
  • create a method call block,
  • copy the method call arguments to the parameters
    in the block,
  • create initial values for any local values used
    by the method, and
  • fill in the return address of the calling method

17
Recursion vs Iteration 2
  • However, recursive algorithms are often easier to
    design then their corresponding iterative
    algorithms
  • In some cases, an inefficient algorithm that is
    easy to understand, develop and maintain is
    preferred to one that is efficient but difficult
    to understand

18
While loops
  • Every tail recursive function can be written in a
    form which Java executed in constant space, using
    explicit looping constructs in Java
  • First form while loops--the most flexible loop
    structure that Java offers is called a while
    loop.
  • Definition 1.30 The while Loop
  • A while loop usually places any
    initialization before the actual loop and the
  • modification statement inside the loop.
    The test together with the keyword
  • while is the essential and mandatory
    part of the loop, using the syntax
  • while (test)
  • codeBlock
  • The test is performed each time before
    codeBlock executes and must
  • evaluate to true before codeBlock
    executes.
  • Figure 1.29 Representation
    of a while loop

19
While loops
  • Definition The while Loop
  • A while loop usually places any
    initialization before the actual loop and the
    modification statement inside the loop. The test
    together with the keyword while is the essential
    and mandatory part of the loop, using the syntax
  • Definition 1.30 The while Loop
  • A while loop usually places any
    initialization before the actual loop and the
  • modification statement inside the loop.
    The test together with the keyword
  • while is the essential and mandatory
    part of the loop, using the syntax
  • while (test)
  • codeBlock
  • The test is performed each time before
    codeBlock executes and must
  • evaluate to true before codeBlock
    executes.

20
While loops
  • Syntax
  • while (boolean expression--test or guard)
  • codeBlock--body
  • The test is performed each time before codeBlock
    executes and must evaluate to true before
    codeBlock executes.
  • Definition 1.30 The while Loop
  • A while loop usually places any
    initialization before the actual loop and the
  • modification statement inside the loop.
    The test together with the keyword
  • while is the essential and mandatory
    part of the loop, using the syntax
  • while (test)
  • codeBlock
  • The test is performed each time before
    codeBlock executes and must

21
While loops
22
While loops--counting
  • Ex. 1 Loop controlled by a counter
  • counter 1
  • //initialize loop control variable
  • while (counter lt 5)
  • // test if variable is within limits
  • System.out.print(counter "\t")
  • counter counter 1
  • //increment loop control variable

23
While loops
  • The value of counter is initialized to 1 the
    condition (counter lt5) is true therefore ,the
    value 5 of the counter is output. The counter is
    incremented by 1 to the value 2 the condition
    (counterlt5) is true, so the value of counter is
    output again.

24
While loops
  • The process continues while the condition
    (counterlt5) remains true. When counter is
    incremented to the value 6, then the condition
    (counterlt5) becomes false and the computer
    executes the while loop.
  • Rewrite the program to count backwards from 5
    down to 1

25
static int fact (int n) ...
  • int count, fprod
  • count n
  • fprod 1// invariant fprod count! n!
  • while (count ! 0)
  • fprod fprod count
  • count count - 1
  • return fprod

26
While loops--sum of even integers less than 500
  • int evenSum 0
  • int i 2
  • while (i lt 500)
  • evenSum i
  • i 2
  • Definition 1.30 The while Loop
  • A while loop usually places any
    initialization before the actual loop and the
  • modification statement inside the loop.
    The test together with the keyword
  • while is the essential and mandatory
    part of the loop, using the syntax
  • while (test)
  • codeBlock

27
While loops--sum of even integers less than 500
  • int evenSum 0
  • int i 2
  • while (i lt 500)
  • evenSum i
  • i 2
  • Definition 1.30 The while Loop
  • A while loop usually places any
    initialization before the actual loop and the
  • modification statement inside the loop.
    The test together with the keyword
  • while is the essential and mandatory
    part of the loop, using the syntax
  • while (test)
  • codeBlock
Write a Comment
User Comments (0)
About PowerShow.com