Upload
valentine-hines
View
215
Download
3
Embed Size (px)
Citation preview
Fundamentals of Software Development 1
Slide 1
LoopsLoops• A A looploop is: is:
– a block of code that a block of code that executes repeatedly while executes repeatedly while some condition holds true.some condition holds true.
• Java provides three forms Java provides three forms for explicit loops:for explicit loops:– whilewhile– forfor– do..whiledo..while
• The The conditionsconditions in loops in loops are written as in are written as in ifif statementsstatements
• See examples on next See examples on next slidesslides
while (condition) { statement; … statement;}
for (start; condition; step) { statement; … statement;}
do { statement; … statement;} while (condition);
Fundamentals of Software Development 1
Slide 2
for-loop examplefor-loop example• Problem: Display the sine of 0.01, sine of 0.02, … sine Problem: Display the sine of 0.01, sine of 0.02, … sine
of 4.00.of 4.00.• Solution idea: Use a counting variableSolution idea: Use a counting variable
– What should the variable start at?What should the variable start at?– The loop should continue while …?The loop should continue while …?– Each time through the loop, increment the variable by …?Each time through the loop, increment the variable by …?
for (double x = 0.01; x <= 4.00; x = x + 0.01) {
System.out.println(Math.sin(x));
}
for (int k = 1; k <= 400; ++k) {
System.out.println(Math.sin(k/100.0));
}Alternative solution:
Both solutions are correct;the first risks round-off error in its
stopping condition
Fundamentals of Software Development 1
Slide 3
while-loop while-loop exampleexample
• Problem:Problem: Input numbers from the console, Input numbers from the console, displaying the sine of each, stopping when displaying the sine of each, stopping when the user inputs a negative numberthe user inputs a negative number
• Three different approachesThree different approaches to solving this to solving this problem:problem:– Use a Use a whilewhile loop that runs forever, loop that runs forever,
• but but breakbreak when a negative number is input when a negative number is input
– First do an input,First do an input,• then use a then use a whilewhile loop that runs while inputs are loop that runs while inputs are
nonnegativenonnegative
– As above, but using a As above, but using a for for looploop
You may see You may see breakbreak statement statement sometimes… this allowes you sometimes… this allowes you to break out of the loopto break out of the loop
Fundamentals of Software Development 1
Slide 4
• Problem: Input numbers from the console, displaying the sine Problem: Input numbers from the console, displaying the sine of each, stopping when the user inputs a negative numberof each, stopping when the user inputs a negative number
while (true) { input = inputStream.nextDouble(); if (input < 0) { break; } System.out.println(Math.sin(input));}
Both solutions are correct.The first emphasizes the stopping condition, at the expense of an awkward start (repeating the same statement before and within the loop).The first solution could be converted to a for-loop easily.
Scanner inputStream = new Scanner(System.in);double input;
input = inputStream.nextDouble();while (input >= 0) { System.out.println(Math.sin(input); input = inputStream.nextDouble();}
This first, then either of the next two boxes.
Fundamentals of Software Development 1
Slide 5
forfor loops versus loops versus whilewhile loops loops
• Typically we use:Typically we use:– forfor when we know in advance how many times the loop will when we know in advance how many times the loop will
executeexecute– whilewhile when something that happens in the loop determines when when something that happens in the loop determines when
the loop exitsthe loop exits– do..whiledo..while when we want a when we want a whilewhile loop that always does at least loop that always does at least
one iterationone iteration
for (int i = 0; i < 7; i = i + 1) { System.out.println (i + " " + i*i);}
int i = 0;while (i < 7) { System.out.println (i + " " + i*i); i = i + 1;}
The two boxes are equivalent.