Chapter 6: Iteration
Nested Loops
- Example: Print the multiplication table for the numbers
from 1 through 9.
Processing Sentinal Values
Scanner in = new Scanner(System.in);
String input;
System.out.print("Enter data: ");
input = in.next();
while (!input.equals("quit"))
{
// process input
System.out.print("Enter data: ");
input = in.next();
}
Random Numbers
Random generator = new Random();
int someInteger = generator.nextInt();
double someDouble = generator.nextDouble();
double someDouble = Math.random();
Simulations
- Buffon's needle experiment (described in book).
- Rolling a six-sided die.
- Using the unit circle to estimate the value for pi.
Lecture Code