COMP 110 Loops, loops, loops, loops, loops, loops… Michele Weigle - COMP 14 - Spr 04 COMP 110 Loops, loops, loops, loops, loops, loops… Luv Kohli September 22, 2008 MWF 2-2:50 pm Sitterson 014
Michele Weigle - COMP 14 - Spr 04 Announcements Program 2 due date extended to Friday, Sep 26, 2pm Lab 4 due date extended to Wednesday, Oct 1, 2pm Print out your programs and hand the in Interesting talk today at 4pm, SN011 William Swartout, “Toward the Holodeck: Integrating Graphics, Artificial Intelligence, Entertainment and Learning”
Michele Weigle - COMP 14 - Spr 04 Questions? How is Program 2 going? Any other questions?
Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 More loop examples If time: indentation
Designing a loop Initializing statements Loop body Ending a loop
Designing a loop body Example: calculate the sum of numbers entered by the user
Identify the loop body from pseudocode Output instructions to the user Initialize variables Prompt user for input Read a number into variable next sum = sum + next; ... Output the sum Repeated statements become your loop body Statements that are only done once are not part of your loop body
Pseudocode with loop body Output instructions to the user Initialize variables Do the following for the appropriate number of times: { Prompt user for input Read a number into variable next sum = sum + next; } Output the sum Initializing statements How do we end the loop? How many iterations?
Initializing statements Variables used in your loop need to be initialized (set to a value) before the loop next Read a number into variable next We read a new value for next before using it during each iteration of the loop so we do not need to initialize it sum sum = sum + next; sum is on the right side of an assignment statement. sum MUST have a valid value before the loop starts.
What should sum’s initial value be? Output instructions to the user Initialize variables Do the following for the appropriate number of times: { Prompt user for input Read a number into variable next sum = sum + next; } Output the sum
Initialize sum Consider the first iteration. After executing the first iteration, the expected value of sum should be sum == next (the first input value) The assignment statement is sum = sum + next; Therefore, initial value of sum is sum = 0;
What should sum’s initial value be? Output instructions to the user sum = 0; Do the following for the appropriate number of times: { Prompt user for input Read a number into variable next sum = sum + next; } Output the sum Initializing statements
Initializing statements Michele Weigle - COMP 14 - Spr 04 Initializing statements Always set initial value to 0? What if we calculate the product of all the input values? product = product * next; After the first iteration, we expect product == next (the first input value) Initial value of product? product = 1;
Ending a loop How do we end the loop? How many iterations? Output instructions to the user sum = 0; Do the following for the appropriate number of times: { Prompt user for input Read a number into variable next sum = sum + next; } Output the sum How do we end the loop? How many iterations?
Ending a loop Count-controlled loops User-controlled loops If you know the number of loop iterations for (count = 0; count < iterations; count++) User-controlled loops Ask-before-iterating Sentinel value
Count-controlled loops You know how many iterations in advance. Ask every student in the class for his/her age and calculate the average age sum = 0; for (student 1 to student n) { Ask his/her age sum = sum + age; } average = sum / n;
Count-controlled loops Use a for loop in most cases 37 students in the class: 37 iterations numStudents = 37; sum = 0; for (count = 1; count <= numStudents; count++) { Ask student #count for his/her age sum = sum + age; } average = sum / numStudents;
Count-controlled loops Can also use a while loop, but for loop is easiest way to implement count-controlled loops numStudents = 37; count = 1; sum = 0; while (count <= numStudents) { Ask student #count for his/her age sum = sum + age; count = count + 1; } average = sum / numStudents;
User-controlled loops Also called ask-before-iterating Ask the user if it is time to end the loop Example: add a bunch of numbers Lab 4: when the user enters a negative number (a sentinel value), it’s time to end the loop
User-controlled loops Use while and do-while in most cases do-while If you know that the user wants at least one iteration Ask the user whether the loop should end at the end of the loop body
User-controlled loops: do-while do { // do stuff in your code here Prompt user for input Read a number into variable next sum = sum + next; // ask if we should do another iteration System.out.print(“Continue (yes/no)? ”); answer = keyboard.nextLine(); } while (answer.equalsIgnoreCase(“yes”));
User-controlled loops: while Use while if there could be zero iterations Example, summing numbers: When the user inputs a negative value: End the loop The negative value is not used in calculating the sum If the first input value is negative, no iterations
User-controlled loops: while sum = 0; while (next >= 0) { Prompt user for input Read a number into variable next sum = sum + next; } Is this code correct? What is the initial value of next?
Solution sum = 0; Prompt user for input Read a number into variable next while (next >= 0) { sum = sum + next; } Is this code correct?
Solution, another try sum = 0; Prompt user for input Read a number into variable next while (next >= 0) { sum = sum + next; } Is this code correct?
Example problem Find the lowest and highest ages in the class Use a loop? Yes What goes in the loop body?
Loop body Ask student 1 for age Update min/max ages Ask student 2 for age ... Ask student 37 for age End loop Output min and max ages
Min/max ages Which kind of loop? Let’s use a for loop
Min/max ages int min = 2000; // initialize to large value int max = 0; // initialize to small value for (int count = 1; count <= 37; count++) { Ask student #count for age if (age > max) max = age; if (age < min) min = age; } Output min and max ages
Min/max ages Ages max min 20 23 18 25 12 94 36 … 20 23 25 94 2000 20 20 23 25 94 2000 20 18 12 if (age > max) max = age; if (age < min) min = age;
Michele Weigle - COMP 14 - Spr 04 Wednesday Nested loops Loop bugs