Title: Loops
1Loops
- More loops
- Off-by-one errors
- Infinite loops
- Nested Loops
2Multiplying Numbers
int product 1 int nextNum
Keyboard.readInt() while (nextNum gt 0)
product product nextNum nextNum
Keyboard.readInt() print
(product)
3Cumulative Assignment
int product 1 int nextNum
Keyboard.readInt() while (nextNum gt 0)
product nextNum nextNum
Keyboard.readInt() print
(product)
4Multiplying Positive Numbers
int product 1 int nextNum
Keyboard.readInt() while (nextNum gt 0)
product product nextNum nextNum
Keyboard.readInt() print
(product)
5Multipling N Numbers (edit)
int product 1 int nextNum
Keyboard.readInt() while (nextNum gt 0)
product nextNum nextNum
Keyboard.readInt() print
(product)
6Multipling 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)
7Multipling N Numbers (soln)
int product 1 int n readNumElements() int
counter 0 while (counter lt n) int nextNum
readNum() product nextNum counter
1
8Multipling First N Numbers N! (edit)
???
Factorial(n) n!
1234 n
9Multipling 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
10Multipling 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
11Multipling 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
12Multipling 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
13Better 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
14Better 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
15Incrementing 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
16Incrementing 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
17Checking of Non Equality
int product 1 int n ??? int
prevMultiplier 0 while (prevMultiplier !
n) prevMultiplier
1 product prevMultiplier
System.out.println (product)
1234 n
18Checking 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
19Checking 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
20Counter not changed
int product 1 int n ??? int
prevMultiplier 0 while (prevMultiplier lt
n) product
prevMultiplier System.out.println
(product)
10000..
1234 n
21Counter 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
22Guarding Against Infinite Loops
- Update variable(s) in loop expression.
- Expression must converge to false.
23Decrementing Solution
int product 1 while (n gt 0) product
n n - 1 System.out.println(product)
1nn-1n-2..1
1234 n
24Decrementing Assignment
int product 1 while (n gt 0) product
n n -- System.out.println(product)
n n-1
n--
n n1
n
25Counter-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
26Counter-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.
27Counter-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
28Factorial List (edit)
int n ??? int product 1
while (n gt 0) product n n
- 1 return product
29Factorial 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
30Factorial 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()
31Removing 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()
32Removing 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()
33break 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)