CS 177 Recitation - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

CS 177 Recitation

Description:

The Final Exam schedule is now available. The CS177 Final will be on Friday, May 4th. It will take place in 8:00-10:00am in WTHR 200. While Loops ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 14
Provided by: just154
Category:
Tags: recitation | wthr

less

Transcript and Presenter's Notes

Title: CS 177 Recitation


1
CS 177 Recitation
  • March 23rd, 2007 Week 10
  • Conditional Repetition

2
Announcements
  • Project 4 is due at 9pm on Wednesday, April 4th.
  • Start now if you have not started yet.
  • The Final Exam schedule is now available.
  • The CS177 Final will be on Friday, May 4th.
  • It will take place in 800-1000am in WTHR 200

3
While Loops
  • General form of while looks quite similar to
    the if statement, but they are completely
    different beasts!
  • For if, depending on the result of
    BOOLEAN_TEST, how many times can the
    corresponding statements be executed?
  • One or none
  • For while, depending on the result of
    BOOLEAN_TEST, how many times can the
    corresponding statements be executed?
  • Possibly many times (until the test fails)

4
Example
  • How many times will the statements in the body of
    the loop be executed?
  • sum 0
  • value 0
  • while (value gt 0)
  • value parseFloat (prompt ("Enter value",
    ""))
  • sum sum value
  • Answer 0 (the boolean test fails with the
    initial value)

5
Priming the Loop
  • Notice the redundancy before the loop. Why is
    it done?
  • Can avoid it by just initializing the roll1 and
    roll2 variables to some dummy values (priming),
    so that the first boolean test succeeds
  • Whenever you do priming, keep in mind that the
    correctness of the code for all cases must be
    preserved!

6
Loop Tests
  • A while loop is NOT a do-until loop
  • Opposite to the way we usually think
  • We think
  • keep rolling dice until you get doubles (i.e.,
    roll1 roll2)
  • What must the while loop be?
  • keep rolling dice while you get different faces
    (i.e. while (roll1 ! roll2). . .)
  • The boolean expression must be true to keep you
    in the loop and false to get you out

7
Counter-Driven Loops
  • What if you need to do a fixed number of
    iterations?
  • The test needs to be based on a counter variable
  • The counter is initially set to 0 and is
    incremented on each repetition of the loop body
  • When the counter reaches our fixed number, the
    boolean test fails and the loop terminates

8
Examples
  • Does it have to count up from 0?
  • No
  • repCount 1
  • while (repCount lt 101)
  • document.write(HOWDY! ltbr /gt)
  • repCount repCount 1
  • Does it have to count up at all?
  • No, it can count down
  • count 10
  • while (count gt 0)
  • document.write(count ltbr /gt)
  • count count - 1
  • document.write(BLASTOFF!)

9
More Examples
  • Does it have to increment/decrement by 1?
  • No
  • count 0
  • while (count lt 1000)
  • document.write(HOWDY! ltbr /gt)
  • count count 10
  • count 100
  • while (count gt 0)
  • document.write(count/10 ltbr /gt)
  • count count - 10
  • document.write(BLASTOFF!)

10
Last Example
  • What is stored in the variable product at the
    conclusion of this loop?
  • x parseFloat (prompt("Enter any number",""))
  • n parseFloat (prompt("Enter any integer gt
    0",""))
  • product 1
  • while (n gt 0)
  • product product x
  • n n - 1
  • Answer xn (variable product is the result of n
    multiplications by x)

11
Conclusions from Examples
  • You will usually have a lot of counting options
    (start from 0/start from something else,
    increment/decrement, by 1/by something else)
  • Always choose the most intuitive way and make
    sure that correctness is preserved
  • What is the most common error in counter-driven
    loops?
  • "off by 1" error
  • How can it be avoided?
  • Carefully think about the initial counter value
    and the value at which the boolean test fails
  • From these two values, figure out how many times
    your loop will iterate
  • When processing arrays and strings, indexing is
    easiest if we begin the loop counter at 0

12
Hailstone Sequence
  • Remember the Hailstone Sequence? (Assume that
    isOdd(x) returns true if x is odd and false
    otherwise)
  • while (number ! 4) if (isOdd(number))   
    number number 3 1  else    number
    number / 2  document.write(number )
  • What will be the output if number 7?
  • 22 11 34 17 52 26 13 40 20 10 5 16 8 4

13
Questions?
Write a Comment
User Comments (0)
About PowerShow.com