Download presentation
Presentation is loading. Please wait.
Published byDennis Alexander Modified over 9 years ago
1
Week 6 - Monday
2
What did we talk about last time? while loop examples Lab 5
7
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 } 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 }
8
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++ } int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i); i--; //whoops, should have been i++ }
9
Being off by one is a very common loop error int n = 40; int i = 1; while( i < 40 ) //won’t //reach 40 { System.out.println(i); i++; } int n = 40; int i = 1; while( i < 40 ) //won’t //reach 40 { System.out.println(i); i++; }
10
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++; } int n = 40; int i = 1; while( i >= 40 ) //oops, should be <= { System.out.println(i); i++; }
11
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++; } int n = 40; int i = 1; while( i <= 40 ); //semicolon is wrong { System.out.println(i); i++; }
12
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
14
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
15
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
17
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
18
Way to Progress Ending Point Starting Point for( init; condition; inc ) { statement1; statement2; … statementn; }
19
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 optional for( init; condition; inc ) statement;
20
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++; } int i = 1; while( i <= 100 ) { System.out.println(i); i++; }
21
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); } for( int i = 1; i <= 100; i++ ) { System.out.println(i); }
22
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
23
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 of one fourth of the circle By multiplying by 4, we approximate π y x
25
They work just like while loops The only difference is that they are guaranteed to execute at least once Unlike a while loop, the condition isn’t checked the first time you go into the loop Sometimes this is especially useful for getting input from the user The syntax is a little bit different
26
do { statement1; statement2; … statementn; } while( condition );
28
Examples with for loops and do-while loops
29
Keep reading Chapter 5 of the textbook Keep working on Project 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.