Loops - PowerPoint PPT Presentation

About This Presentation
Title:

Loops

Description:

Title: State and Properties Author: Prasun dewan Last modified by: dewan Created Date: 8/28/2000 4:35:56 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 34
Provided by: Pras47
Learn more at: https://www.cs.unc.edu
Category:
Tags: candies | loops

less

Transcript and Presenter's Notes

Title: Loops


1
Loops
  • More loops
  • Off-by-one errors
  • Infinite loops
  • Nested Loops

2
Multiplying Numbers
int product 1 int nextNum
Keyboard.readInt() while (nextNum gt 0)
product product nextNum nextNum
Keyboard.readInt() print
(product)
3
Cumulative Assignment
int product 1 int nextNum
Keyboard.readInt() while (nextNum gt 0)
product nextNum nextNum
Keyboard.readInt() print
(product)
4
Multiplying Positive Numbers
int product 1 int nextNum
Keyboard.readInt() while (nextNum gt 0)
product product nextNum nextNum
Keyboard.readInt() print
(product)
5
Multipling N Numbers (edit)
int product 1 int nextNum
Keyboard.readInt() while (nextNum gt 0)
product nextNum nextNum
Keyboard.readInt() print
(product)
6
Multipling N Numbers (edited)
int product 1 int nextNum
Keyboard.readInt() while (nextNum gt 0)
product nextNum nextNum
Keyboard.readInt() print
(product)
int listLength readListLength() int counter
0 int nextNum int product 1 while (counter lt
listLength) counter 1 nextNum
readNextNumber() product nextNum print
(product)
7
Multipling N Numbers (soln)
int product 1 int n readNumElements() int
counter 0 while (counter lt n) int nextNum
readNum() product nextNum counter
1
8
Multipling First N Numbers N! (edit)
???
Factorial(n) n!
1234 n
9
Multipling First N Numbers
int product 1 int n 2 int counter 0
while (counter lt n) product
counter counter 1
System.out.println (product)
101
1234 n
10
Multipling First N Numbers
int product 1 int n ??? int counter
0 while (counter lt n) product
counter counter 1
System.out.println (product)
1012 n-1
1234 n
11
Multipling First N Numbers
int product 1 int n ??? int counter
1 while (counter lt n) product
counter counter 1
System.out.println (product)
112 n-1
1234 n
12
Multipling First N Numbers
int product 1 int n ??? int counter
1 while (counter lt n) product
counter counter 1
System.out.println (product)
112 n-1n
1234 n
13
Better Name
int product 1 int n ??? int
nextMultiplier 1 while (nextMultiplier lt
n) product nextMultiplier
nextMultiplier 1
System.out.println (product)
112 n-1n
1234 n
14
Better Name
int product 1 int n ??? int
nextMultiplier 0 while (nextMultiplier lt
n) product nextMultiplier
nextMultiplier 1
System.out.println (product)
101 2 n-1n
1234 n
15
Incrementing Counter Before Operation
int product 1 int n ??? int
prevMultiplier 0 while (prevMultiplier lt
n) prevMultiplier
1 product prevMultiplier
System.out.println (product)
112 n-1nn1
1234 n
16
Incrementing Counter Before Operation
int product 1 int n ??? int
prevMultiplier 0 while (prevMultiplier lt
n) prevMultiplier
1 product prevMultiplier
System.out.println (product)
112 n-1n
1234 n
17
Checking of Non Equality
int product 1 int n ??? int
prevMultiplier 0 while (prevMultiplier !
n) prevMultiplier
1 product prevMultiplier
System.out.println (product)
1234 n
18
Checking of Non Equality
int product 1 int n -5 int
prevMultiplier 0 while (prevMultiplier !
n) prevMultiplier
1 product prevMultiplier
System.out.println (product)
1234 n
19
Checking of Non Equality
int product 1 int n -5 int
prevMultiplier 0 while (prevMultiplier !
n) prevMultiplier
1 product prevMultiplier
System.out.println (product)
-5-4-3-2-1012...
1234 n
20
Counter not changed
int product 1 int n ??? int
prevMultiplier 0 while (prevMultiplier lt
n) product
prevMultiplier System.out.println
(product)
10000..
1234 n
21
Counter changed in wrong direction
int product 1 int n ??? int
prevMultiplier 0 while (prevMultiplier lt
n) prevMultiplier -
1 product prevMultiplier
System.out.println (product)
10-1-2-3
1234 n
22
Guarding Against Infinite Loops
  • Update variable(s) in loop expression.
  • Expression must converge to false.

23
Decrementing Solution
int product 1 while (n gt 0) product
n n - 1 System.out.println(product)
1nn-1n-2..1
1234 n
24
Decrementing Assignment
int product 1 while (n gt 0) product
n n -- System.out.println(product)
n n-1
n--
n n1
n
25
Counter-Controlled Vs Event Controlled
int product 1 int nextNum
Keyboard.readInt() while (nextNum gt 0)
product product nextNum nextNum
Keyboard.readInt() print
(product)
Event-Controlled
int product 1 int n readNumElements() int
counter 0 while (counter lt n) int nextNum
readNum() product nextNum counter
1
Counter-Controlled
26
Counter-Controlled Vs Event- Controlled
  • Number of loop iterations (executions of loop
    body) known before loop executed
  • initialize counter to some value
  • increment/decrement counter by fixed step
    beginning/end of body
  • exit when counter reaches limit.
  • Limit not known before loop starts.
  • Test one more events (e.g. reading of input)
    occurring while loop executes.
  • Terminate when events make loop condition false.

27
Counter-Controlled Vs Event- Controlled
Counting until 10 in Hide Seek
Counter-controlled
Event-controlled
Searching for others in Hide Seek
Counter-controlled
Grading Comp14 exams
Event-controlled
Fishing for the right answer from students
Counter-controlled
Counting the days until it is vacation
Counter-controlled
Counting of candies in a jar
Event-controlled
28
Factorial List (edit)
int n ??? int product 1
while (n gt 0) product n n
- 1 return product
29
Factorial List (edited)
int newVal Keyboard.readInt() while (newVal
gt 0) int product 1 n
newVal while (n gt 0) product
n n - 1 return product
30
Factorial List (soln)
public static int factorial (int n) int
product 1 while (n gt 0)
product n n - 1 return
product
public static void main (String args) int n
Keyboard.readInt() while (n gt 0)
System.out.println(factorial
factorial(n)) n Keyboard.readInt()
31
Removing Code Duplication (edit)
public static void main (String args) int n
Keyboard.readInt() while (n gt 0)
System.out.println(factorial
factorial(n) n Keyboard.readInt()
32
Removing Code Duplication (edited)
public static void main (String args) int n
Keyboard.readInt() while (n gt 0)
System.out.println(factorial
factorial(n) n Keyboard.readInt()
33
break Statement
public static int factorial (int n) int
product 1 while (n gt 0)
product n n - 1 return
product
public static void main (String args) while
(true) // loop condition never false int n
Keyboard.readInt() if (n lt 0)
break System.out.println(factorial
factorial(n)
Write a Comment
User Comments (0)
About PowerShow.com