Interactive Powerpoint_How to Master effective communication
more loops lecture by Professor Evan korth
1. Introduction to Computers and
Programming
Lecture 8: More Loops
Professor: Evan Korth
New York University
2. 2
Upcoming schedule
• February 21: lecture 8; hw#2 due
• February 26: lecture 9
• February 28: lecture 10; start review; hw#3 due
• March 5: review
• March 7: midterm
• March 13, 15: Spring break (yay!)
3. Road Map
• Sentinels
• Sentinel Controlled Loops
• Case Study: Sentinel Controlled Loop
• Case Study: Nested Control Structures
• do/while Loop
• Summary of looping covered so far
• Reading
– Liang 5:chapter 3: 3.3.1, 3.3.2
– Liang 6: chapter 4: 4.2, 4.3
4. review
• What are three ways to rewrite:
x = x + 1;
• What are the three elements of a while loop?
• What is the difference between pre and post
increment operators?
• Are you guaranteed to execute the body of a while
loop at all?
• What is an infinite loop?
• True or False: You never want to use an infinite
loop?
5. 5
Review
• Given:
char c = 'a';
c++;
What is the value of variable c?
• What method do you use to extract characters
from a String?
– How would you use that method to get the first character
from a String?
• How are characters stored in memory?
• What is the difference between the character '0'
and the integer 0?
7. Sentinel-controlled repetition
• Counter Controlled Repetition:
– Simply means that we use a counter to tell you when to stop
repeating a statement or group of statements
– The examples from last class were counter-controlled
repetition
• However, what if we want the user or the input to
decide when to end the program?
– Use a sentinel
9. Using Sentinels
• How are they used?
– Programmer picks a value that would never be encountered
for normal data
– User enters normal data and then when done, enters the
sentinel value
– The loop will stop when seeing the sentinel value
10. Using Sentinels cont’d
• For example, if entering age for people, could pick
a sentinel of –1
• No one would expect to be –1 year old.
• Good practice is to remind the user in each
iteration of the loop what the sentinel value is
– For example,
System.out.println
(" Enter age of current resident or –1 to end" );
11. /* A sentinel controlled loop */
/* A simple census taker */
import javax.swing.JOptionPane;
public class Sentinel
{
public static void main(String [] args)
{
int currentAge = 0 ;
String currentAgeAsString;
/* priming */
currentAgeAsString = JOptionPane.showInputDialog ("Enter age of resident: ") ;
currentAge = Integer.parseInt (currentAgeAsString);
/* testing: keep going until input is sentinel value */
while (currentAge != -1)
{
/* do some calculations with age, e.g. AVERAGE */
/*updating: get the next value from the user */
currentAgeAsString = JOptionPane.showInputDialog ("Enter age of resident: ") ;
currentAge = Integer.parseInt (currentAgeAsString);
}
System.exit (0);
}
}
12. Good Programming tips
• Pick a sentinel value that you are CERTAIN will
never be confused with normal data
• Style: Remind user each iteration what the sentinel
is
• Y2K-like problem
– Programmers often used 9999 as a sentinel to end a loop
– Worry that on September 9, 1999 (sometimes abbreviated
9999) programs would erroneously stop executing before
they were supposed to.
30. The do/while Repetition Structure
• The do/while repetition structure
– Similar to the while structure
– Condition for repetition tested after the body of the loop is
performed
• All actions are performed at least once
– Format:
do {
statement(s);
} while ( condition );
31. 4.8 The do/while Repetition Structure
• Flowchart of the do/while repetition structure
true
false
action(s)
condition
32. 32
public class DoWhileTest
{
public static void main(String args[])
{
int counter;
counter = 1;
do
{
System.out.println ("counter: "+
counter);
counter ++;
} while (counter <= 10);
}
}
34. Summary of Looping
• Two broad types of loops:
– Counter-controlled repetition
• A counter controls the number of repetitions.
• Also known as a definite repetition, because we know in
advance how many times the loop will be executed.
– Sentinel-controlled repetition
• A sentinel controls the number of repetitions
• Also known as indefinite repetition, because we do not know
in advance how many times the loop will be executed.
• In either case, watch out for infinite loops!
• If your program requires some kind of loop, first
determine which kind of loop you want.
35. Summary of Looping
• Once you know which kind of loop you want,
determine which while loop you want:
– While loops
• condition is tested first; then action occurs.
• While loops are much more common than do/while loops.
– Do/while loops
• action is run first; then, condition is tested.
• Use this if you want to make sure that your loop is guaranteed
to be run at least once.