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
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

Week 6 - Monday

slide-2
SLIDE 2

 What did we talk about last time?  while loop examples

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
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
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 7
slide-8
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
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
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
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
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
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 14
slide-15
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
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 17
slide-18
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
SLIDE 19

Way to Progress Ending Point Starting Point

for( init; condition; inc ) { statement1; statement2; … statementn; }

slide-20
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
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
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
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
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 25
slide-26
SLIDE 26

 do-while loops  Examples with for loops and do-while loops

slide-27
SLIDE 27

 Keep reading Chapter 5 of the textbook  Keep working on Project 2

  • Due Friday!