Iteration - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Iteration

Description:

iteration. 4. Bohm and Jacopini. This time we are interested in the third construct. It is called iteration which means do again. Its basic form is the while loop ... – PowerPoint PPT presentation

Number of Views:170
Avg rating:3.0/5.0
Slides: 15
Provided by: calpoly
Category:
Tags: iteration

less

Transcript and Presenter's Notes

Title: Iteration


1
Iteration
  • Robert Stumpf, Professor
  • Computer Information Systems
  • California State Polytechnic University Pomona

2
Iteration
  • Bohm and Jacopini
  • Iteration
  • Forms
  • while loop
  • for loop
  • do while loop

3
Bohm and Jacopini
  • Bohm and Jacopini stated that a Structured
    program contains three constructs
  • 1. Sequence
  • 2. Selection
  • 3. Iteration
  • They proved this mathematically

4
Bohm and Jacopini
  • This time we are interested in the third
    construct
  • It is called iteration which means do again
  • Its basic form is the while loop
  • The other forms are convenient but can be
    simulated if not present

5
Iteration
  • Iteration means to repeat the process
  • A while loop has the test at the beginning
  • Question resulting in a boolean response
  • Process to be repeated
  • If question is true, repeat, if false exit loop

6
Iteration
  • A do while loop has the test at the end
  • do
  • Process to be repeated
  • Question resulting in a boolean response
  • If question is true, repeat, if false exit loop

7
Forms
  • Basic While (without a block) int count 0
    while ( count lt 3 )
  • System.out.println(count )
  • Not using a block is not very useful as only one
    statement is permitted

8
Forms
  • Basic While (with a block) int count 0
    while ( count lt 3 )
  • System.out.println(count)
  • count
  • Using a block is better
  • The question is asked at the beginning

9
Forms
  • Using count Is same as count count
    1
  • count -- is also legal
  • Its precedence is at the first level along with !
    and (unary)

10
Forms
  • Basic For (without a block) for (int i0 i lt
    3 i )
  • System.out.println(count)
  • The question still is asked at the beginning

11
Forms
  • Basic For (with a block) for (int i0 i lt 3
    i )
  • System.out.println(count)
  • Using a block is better why?

12
Forms
  • Do While
  • char responsedo
  • // code to input, process, and output is here
    response JOptionPane.showInputDialog
    (Enter N if done Y if you wish to do it again
    )
  • while (response.charAt(0) Y)
  • Question is asked at the end

13
Summary
  • Java allows one to iterate three ways
  • while loop test at the beginning
  • for loop test at the beginning
  • do while loop test at the end
  • The for loop is for convenience as there are many
    situations requiring one to count

14
Thank You
  • Java has Built in Constructs to facilitate
    Iteration
  • Any questions should be directed to Professor
    Robert Stumpf
  • Email rvstumpf_at_csupomona.edu
  • Web Sitehttp//www.csupomona.edu/rvstumpf
Write a Comment
User Comments (0)
About PowerShow.com