COMP 111 Introduction to Computer Science and Object-Oriented Programming 5
Feeling Out of the Loop?
Methods - So Far Execute a sequence of statements Declarations Assignment statements Return Maybe an output (e.g. System.out.println ) Execute in order First to last Until last is executed Or a return Or an error And from last week. With Expressions!
Methods - So Far Optionally execute a sequence of statement Alternatively execute this sequence of statement or that sequence of statement With variations Series of comparisons Nested comparisons With Conditons!
But While (as humans) we know how to Determine when an interest-bearing investment reaches a target amount Do long division with remainders Average the scores of any 111 class on Test 1 We cannot write methods that do these! (or at least not easily and in general!)
They All Hinge on Repeating Describe how repetition drives Determining when an interest-bearing investment reaches a target amount Doing long division with remainders Averaging the scores of any 111 class on Test 1
Digging Deeper In each case we repeat something gated by some critical test repeat something as long as until a test is true!
They All Hinge on a Test Describe what key test drives Determining when an interest-bearing investment reaches a target amount Doing long division with remainders Averaging the scores of any 111 class on Test 1
Examples from the World As long as the meat thermometer is less than 160 degrees, bake another 5 minutes While the robot does not sense the wall ahead, move it forward one unit while a test is true statement whatever is next true test statement statement false
Examples from the World Keep going straight until you reach Putnam street Continue adding stuff while the weight is less than 50 lbs do statement while a test is true go on to next true statement statement test false
Examples from the World Sum the usage minutes for each day of the year Compute 33! (33 factorial) initialize counter for (a range of counter values) statement go on to next test counter true statement statement false adjust counter
The Java While Loop Want to repeat while something is true while (test) statements true test false the loop body statement statement Must the loop body be executed at least once?
Consider Compute integer division the hard way count how many times the divisor can be subtracted from the dividend A small aha fits this into an iteration framework? dividend divisor count 13 4 0 why stop?
Moving to the While Compute integer division the hard way count how many times the divisor can be subtracted from the dividend dividend divisor count 13 4 0 9 4 1 5 4 2 1 4 3 start the count at 0; while (the dividend >= than divisor) subtract the divisor; increment the count;
Realizing In Java Want to repeat while something is true public int divideby(int num1, int num2) int count = 0; int dividend = num1; while (dividend >= num2) dividend = dividend - num2; count++; return count;
You Try It? public int divideby(int num1, int num2) int count = 0; int dividend = num1; Trace the value for divideby(20,5) divideby(3,5) divideby(7,7) while (dividend >= num2) dividend = dividend - num2; count++; return count; dividend divisor count 0
The Gotcha! public int divideby(int num1, int num2) int count = 0; int dividend = num1; Trace the value for divideby(5,0) divideby(7,-2) while (dividend >= num2) dividend = dividend - num2; count++; return count; dividend divisor count 0
Another Gotcha! Off-by-one errors! public int divideby(int num1, int num2) int count = 0; int dividend = num1; Bad Test while (dividend > num2) dividend = dividend - num2; count++; return count; Try divideby(4,2) dividend divisor count 0
The Java Do Loop Always executes the body of the loop At least once! do statements while (test); true statement statement test the loop body false
Consider Compute mystery math operation public int mystery(int m, int n) int r; do r = n % m; n = m; m = r; while (r!= 0); mystery(8,20) r n m 20 8 4 8 4 0 4 0 return n;
Consider Compute mystery math operation public int mystery(int m, int n) int r; do r = n % m; n = m; m = r; while (r!= 0); mystery(20,8) r n m 8 20 8 20 8 4 8 4 0 4 0 return n;
Consider Compute mystery math operation public int mystery(int m, int n) int r; do r = n % m; n = m; m = r; while (r!= 0); mystery(3,11) r n m 11 3 2 3 2 1 2 1 0 1 0 return n;
What Is It? Can you name the mystery operation mystery(20,8) = 4 mystery(8,20) = 4 mystery(3,11) = 1
Another Problem What is the sum of the first n positive odd integers? For example the first 5 positive integers are 1, 3, 5, 7, 9 their sum is 25 easy, but what about sum of first 1000?
A Strategy What is the sum of the first n positive odd integers? Start with odd number 1 with a sum of 0 Repeat n times add the odd number to the sum find the next odd number
Realizing the Strategy What is the sum of the first n positive odd integers? Start with odd number 1 with a sum of 0 Repeat n times add the odd number to the sum find the next odd number start the odd number at 1 start the sum at 0; while (less than n times) add the odd number to the sum; find next odd number;
Moving Forward What is the sum of the first n positive odd integers? Start with odd number 1 with a sum of 0 Repeat n times add the odd number to the sum find the next odd number start the odd number at 1 start the sum at 0; while (less than n times) add the odd number to the sum; find next odd number; int oddnum = 1; int sum = 0; while (less than n times) sum = sum + oddnum; oddnum = oddnum + 2;
Thinking About Counting How do you count n times? start a count at 0 while (count < n) add 1 to the count; int count = 0; while (count < n) count++; Note: when n is 5, this counts 0, 1, 2, 3, 4 How would you count 1, 2, 3, 4, 5?
Putting It Together What is the sum of the first n positive odd integers? start the odd number at 1 start the sum at 0; while (less than n times) add the odd number to the sum; find next odd number; int count = 0; while (count < n) count++; int count - 0; int oddnum = 1; int sum = 0; while (count < n) sum = sum + oddnum; oddnum = oddnum + 2; count++;
In a Method public int sumfirstodds(int n) int count = 0; int oddnum = 1; int sum = 0; sumfirstodds(5) n count oddnum sum while (count < n) sum = sum + oddnum; oddnum = oddnum + 2; count++; return sum;
You Try It public int sumfirstodds(int n) int count = 0; int oddnum = 1; int sum = 0; sumfirstodds(1) n count oddnum sum while (count < n) sum = sum + oddnum; oddnum = oddnum + 2; count++; return sum;
Counting Is Common Counting up, counting down, etc. Using a counter (or index) int count = 0; while (count < n) count++;
The Java For Loop Abbreviates the initialization testing adjusting of a counter Implicitly executes them implicit implicit initialize counter test counter false for (init, testing, adjusting) statement true statement statement implicit adjust counter
The Java For Loop Abbreviates the initialization testing adjusting of a counter implicit implicit int count; for (count = 0; count < n; count++) statements implicit count = 0; true count < n statement statement count++ false
Notes the For Loop Note the semicolons Can declare counter within the for construct for (int count = 0; count < n; count++) statements Does the for replace the while loop?
Using the For Loop public int sumfirstodds(int n) int oddnum = 1; int sum = 0; sumfirstodds(5) n count oddnum sum for (count = 0; count < n; count++) sum = sum + oddnum; oddnum = oddnum + 2; return sum;
Would This Work? public int sumfirstodds(int n) int oddnum = 1; int sum = 0; sumfirstodds(5) n count oddnum sum for (count = 1; count <= n; count++) sum = sum + oddnum; oddnum = oddnum + 2; return sum;