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 do a lot of work
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 n th 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
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 }
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 }
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; }
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; }
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; }
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
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
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
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: Initialization 1. 2. Condition Increment 3.
Starting Point Way to Progress for( init; condition; inc ) { statement1; statement2; Ending … Point statementn; }
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 for( init; condition; inc ) statement; Then, like if -statements and while -loops, the braces are optional
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; }
A for loop is specifically designed for this sort of thing: for( int i = 1; i <= 100; ++i ) { System.out.println(i); } The initialization and the increment are built-in
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
We can do something called a Monte Carlo approximation of π y 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 x thrown That fraction is an estimation of the area of one fourth of the circle By multiplying by 4, we approximate π
do-while loops Examples with for loops and do-while loops
Keep reading Chapter 5 of the textbook Keep working on Project 2 Due Friday!
Recommend
More recommend