SLIDE 1
Week 6 - Monday What did we talk about last time? while loop - - PowerPoint PPT Presentation
Week 6 - Monday What did we talk about last time? while loop - - PowerPoint PPT Presentation
Week 6 - Monday What did we talk about last time? while loop examples Just as with if -statements, it's possible to nest loops A repetitive task can be done inside of another repetitive task Be careful! You can make the computer
SLIDE 2
SLIDE 3
SLIDE 4
SLIDE 5
Just as with if-statements, it's possible to nest loops A repetitive task can be done inside of another repetitive task Be careful! You can make the computer do a lot of work
SLIDE 6
Triangular numbers are 1, 3, 6, 10, …
- 1 = 1
- 3 = 1 + 2
- 6 = 1 + 2 + 3
- 10 + 1 + 2 + 3 + 4
Let's write a program that expresses the nth triangular number
by printing 1 on the first line, 1 and 2 on the second line, 1, 2, and 3 on the third line, and so on
SLIDE 7
SLIDE 8
Loops can go on forever if you aren't careful
int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i); // Supposed to print all the numbers // less than 40, but i never increases }
SLIDE 9
Overflow and underflow will make some badly written loops
eventually terminate
int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i);
- -i; // Whoops, should have been ++i
}
SLIDE 10
Being off by one is a very common loop error
int n = 40; int i = 1; // Won't reach 40 while( i < 40 ) { System.out.println(i); ++i; }
SLIDE 11
If the condition isn't true to begin with, the loop will just be
skipped
int n = 40; int i = 1; while( i >= 40 ) { // Oops, should be <= System.out.println(i); ++i; }
SLIDE 12
A misplaced semicolon can cause an empty loop body to be
executed (often infinitely)
int n = 40; int i = 1; while( i <= 40 ); { // Semicolon is wrong System.out.println(i); ++i; }
SLIDE 13
The condition of the while loop is not followed by a
semicolon
Be careful about starting and ending conditions When in doubt, use braces The print statement must be inside the loop in order to get
printed multiple times
There's no magic formula; you have to think it through
SLIDE 14
SLIDE 15
- 1. while loops
- Used when you don't know how many times you are going to need
to repeat
- 2. for loops
- Used when you do know how many times you are going to repeat
- 3. do-while loops
- Used never
- Oh, okay, they are used whenever you need to be guaranteed the
loop runs at least once
SLIDE 16
Any problem that uses loops can use any kind of loop The choice is supposed to make things easier on the
programmer
Some loops are more convenient for certain kinds of problems
SLIDE 17
SLIDE 18
for loops are great when you know how many times a loop
will run
They are the most commonly used of all loops They are perfect for any task that needs to run, say, 100 times A for loop has 3 parts in its header: 1.
Initialization
- 2. Condition
3.
Increment
SLIDE 19
Way to Progress Ending Point Starting Point
for( init; condition; inc ) { statement1; statement2; … statementn; }
SLIDE 20
A for loop will usually have multiple statements in its body However, it is possible to make a for loop with only a single
statement
Then, like if-statements and while-loops, the braces are
- ptional
for( init; condition; inc ) statement;
SLIDE 21
Let's print the numbers from 1 to 100 (again) Remember how this was done with while:
int i = 1; while( i <= 100 ) { System.out.println(i); ++i; }
SLIDE 22
A for loop is specifically designed for this sort of thing: The initialization and the increment are built-in
for( int i = 1; i <= 100; ++i ) { System.out.println(i); }
SLIDE 23
Ask the user to input a positive integer n Now, write a for loop to print out the first n odd numbers Example:
If the user enters 10, print out: 1 3 5 7 9 11 13 15 17 19
SLIDE 24
We can do something called a Monte
Carlo approximation of π
We "throw" darts at a 1 x 1 square in the
upper right corner of a circle with radius 1
We count the ones that fall inside the
circle and divide by the total darts thrown
That fraction is an estimation of the area
- f one fourth of the circle
By multiplying by 4, we approximate π
y x
SLIDE 25
SLIDE 26
do-while loops Examples with for loops and do-while loops
SLIDE 27
Keep reading Chapter 5 of the textbook Keep working on Project 2
- Due Friday!