Iteration - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Iteration

Description:

Iteration. While loops. Often we want to continue to do the same ... Stylistic issues. Always use {} for(int i = 0; i 10; i ) ; total = total (total*i) ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 21
Provided by: digit63
Category:

less

Transcript and Presenter's Notes

Title: Iteration


1
Iteration
2
While loops
  • Often we want to continue to do the same thing
    many times, until some condition has been met
  • While the user continues to enter input, keep
    processing it
  • while(!input.equals(QUIT))
  • System.out.println(You entered input)
  • input console.readLine()

3
While flowchart
  • while(condition)
  • statements

!input.equals(QUIT)
False
True
System.out.println(You entered input)
input console.readLine()
4
Infinite loops
  • while(!input.equals(QUIT))
  • System.out.println(You entered input)
  • This loop never ends!
  • We never advance the variable that controls the
    loop (input)

5
Common forms
  • int start 0, total 0
  • while(start lt 10)
  • total total start
  • start
  • Iterate through the loop some number of times
  • increment some counting variable at each iteration

6
For loops
  • Easy shorthand for the most common kind of loop
  • for(initialization condition update)
  • statements

7
While vs For
  • int start 0, total 0
  • while(start lt 10)
  • total total start
  • start

for(int start 0 start lt 10 start) total
total start
8
Other for examples
  • int start
  • for(start 0 start lt 10 start)
  • for(int years n years gt 0 years years 1)
  • for(int rate -10 rate lt 10 rate rate
    .05)
  • for(rate 5 years-- 0 System.out.println(bala
    nce))
  • //bad style

9
Scope of variables
  • for(int start 0 start lt 10 start)
  • total total start
  • The scope of start extends to the end of the loop
  • for(int start 0 start lt 10 start)
  • total total start
  • for(int start 5 start lt 100 start 5)
  • total total - start

10
Stylistic issues
  • Always use
  • for(int i 0 i lt 10 i)
  • total total (totali)
  • Dont use !
  • for(int i 0 i ! n i)
  • total total (totali)
  • Dont update your control variable in the loop
  • for(int i 0 i ! n i)
  • i n i

11
break and continue
  • break lets you get out of a loop early
  • while(true)
  • if(input.equals(QUIT))
  • break
  • System.out.println(You entered input)

12
break and continue
  • continue lets you skip the rest of the loop and
    start again at the top
  • while(a lt b)
  • if(input null)
  • continue
  • System.out.println(You entered input)

13
Nested loops
  • Suppose we want to print out

14
Goal
  • User enters the number of rows and the number of
    columns
  • Print a rectangle (of characters) that matches
    the users specification

15
Examples
  • User enters 1, 3
  • User enters 4, 5

16
How do we do it?
  • find out how many rows the user wants to print
    (numrows)
  • print out numrows rows

17
PrintRect.java
  • public class PrintRect
  • public static void main(String args)
  • for(int i 0 i lt numrows i) //print a row

18
How do we print a row?
  • Find out how many columns each row should have
    (numcols), and print a star for each column
  • for(int j 0 j lt numcols j)
    System.out.print()

19
PrintRect.java
  • public class PrintRect
  • public static void main(String args)
  • for(int i 0 i lt numrows i) //print a row

20
PrintRect.java
  • public class PrintRect
  • public static void main(String args)
  • for(int i 0 i lt numrows i)
  • for(int j 0 j lt numcols j)
    System.out.print()
Write a Comment
User Comments (0)
About PowerShow.com