Download presentation
Presentation is loading. Please wait.
Published byΚαλλικράτης Μιαούλης Modified over 6 years ago
1
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
2
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”
3
Michele Weigle - COMP 14 - Spr 04
Questions? How is Program 2 going? Any other questions?
4
Michele Weigle - COMP 14 - Spr 04
Today in COMP 110 More loop examples If time: indentation
5
Designing a loop Initializing statements Loop body Ending a loop
6
Designing a loop body Example: calculate the sum of numbers entered by the user
7
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
8
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?
9
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.
10
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
11
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;
12
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
13
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;
14
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?
15
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
16
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;
17
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;
18
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;
19
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
20
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
21
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”));
22
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
23
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?
24
Solution sum = 0; Prompt user for input Read a number into variable next while (next >= 0) { sum = sum + next; } Is this code correct?
25
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?
26
Example problem Find the lowest and highest ages in the class
Use a loop? Yes What goes in the loop body?
27
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
28
Min/max ages Which kind of loop? Let’s use a for loop
29
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
30
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;
31
Michele Weigle - COMP 14 - Spr 04
Wednesday Nested loops Loop bugs
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.