Week 5 - Friday What did we talk about last time? Exam 1 post - - PowerPoint PPT Presentation

week 5 friday what did we talk about last time exam 1
SMART_READER_LITE
LIVE PREVIEW

Week 5 - Friday What did we talk about last time? Exam 1 post - - PowerPoint PPT Presentation

Week 5 - Friday What did we talk about last time? Exam 1 post mortem Repetition while loops We can also use while loops to help us deal with input What if we wanted to sum all of the numbers that a person entered? How


slide-1
SLIDE 1

Week 5 - Friday

slide-2
SLIDE 2

 What did we talk about last time?  Exam 1 post mortem  Repetition  while loops

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

 We can also use while loops to help us deal with input  What if we wanted to sum all of the numbers that a person

entered?

 How would we know when to stop?  One solution is to keep adding numbers until the person

enters a negative number

 This is called using a sentinel value

slide-7
SLIDE 7

 Solution:

int i = 0; int sum = 0; Scanner in = new Scanner( System.in ); while( i >= 0 ) { sum += i; System.out.print("Enter number: "); i = in.nextInt(); } System.out.println("Sum: " + sum);

slide-8
SLIDE 8

 We could also find the average:

int i = 0; double sum = 0; int count = 0; Scanner in = new Scanner( System.in ); while( i >= 0 ) { sum += i; ++count; System.out.print("Enter number: "); i = in.nextInt(); }

  • -count; //fixes extra count for sentinel

System.out.println("Average: " + (sum / count));

slide-9
SLIDE 9

 What if we wanted to find the biggest input?  Somehow we would have to check each input and see if it

were larger than the current largest

 Solution: use an if-statement inside of a while loop  Let's look at Eclipse

slide-10
SLIDE 10

 Let's say that you wanted to write a program to guess a

number that a person had come up with

 The number is between 1 and 100  Every time the computer guesses a number, the person

enters:

  • H

if the number is too high

  • L

if the number is too low

  • F

if the number was found

slide-11
SLIDE 11
  • 1. Start with the minimum and maximum of the range
  • 2. Find the midpoint
  • 3. Ask the user if the midpoint is correct
  • 4. If the answer is too high, go to Step 1 using the minimum

and the midpoint as the new range

  • 5. If the answer is too low, go to Step 1 using the midpoint and

the maximum as the new range

  • 6. If the midpoint is correct, you're done!
slide-12
SLIDE 12

 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-13
SLIDE 13

 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-14
SLIDE 14
slide-15
SLIDE 15
slide-16
SLIDE 16

 for loops  More loop examples

slide-17
SLIDE 17

 Keep reading Chapter 5  Keep working on Project 2