Computer Science 111 Fundamentals of Computer Programming I - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Computer Science 111 Fundamentals of Computer Programming I

Description:

Forget to initialize accumulator has old value, etc. Initialize inside loop accumulator doesn't accumulate. Terminating condition ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 14
Provided by: tomwh
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 111 Fundamentals of Computer Programming I


1
Computer Science 111Fundamentals of Computer
Programming I
  • Control Statements
  • The While Statement

2
(No Transcript)
3
(No Transcript)
4
The While-Statement
  • The syntax for the While-Statement is
    while (condition) statement
  • The statement may be compound - more than one
    statement enclosed in a pair of

5
While-Statement example
  • writer.println(Enter 50 numbers)count0
    total0while (count lt 50)
    writer.print(Enter number ) num
    reader.readDouble() total total
    num count count1 writer.print(The
    total is )writer.println(total)

6
Common While Structure
  • initialize accumulatorinitialize counterwhile
    (cond about counter) get some input
    do computation update accumulator
    increment counter output results

7
Common Mistakes
  • Initialization
  • Forget to initialize accumulator has old value,
    etc.
  • Initialize inside loop accumulator doesnt
    accumulate
  • Terminating condition
  • Use lt instead of lt, etc. Off by one errors.
  • Loop body
  • Forget to increment, etc. Infinite loop.
  • Output
  • Inside loop outputs every time through loop

8
Example
  • We want a program that takes as input a positive
    number, limit, and outputs how many consecutive
    integers, starting at 1 that we can add together
    without exceeding limit.
  • For example, if limit is 25 the output should be
    6 since 12345621 and 123456728.

9
Example cont.
  • We will need variables
  • limit
  • sum for the running total
  • current for the number we are currently adding to
    sum
  • We should initialize sum to 0, current to 1
  • We get the value for limit from the user before
    the loop.

10
Example cont.
  • int limit, current1,sum0limit
    reader.readInt(Enter upper limit )while (sum
    lt limit) sum sum current
    current current 1writer.print(Answer is
    )writer.println(current)

11
Example cont.
  • limit 25iteration current sum0
    1 01 2
    12 3
    33 4 64
    5 105
    6 156 7
    217 8
    28
  • Output is 8 - Wrong
  • int limit, current1,sum0limit
    reader.readInt(Enter upper limit )while
    (sum lt limit) sum sum current
    current current 1writer.print(Answer is
    )writer.println(current)

12
Example cont.
  • int limit, current1,sum0limit
    reader.readInt(Enter upper limit )while
    (sum lt limit) sum sum current
    current current 1writer.print(Answer is
    )writer.println(current)
  • writer.println(current-1)
  • We still have a problem try limit 15 for
    example.
  • lt should be lt

13
Ok boys, there's a bunch of those furry
creatures, see if you can lasso them...
Write a Comment
User Comments (0)
About PowerShow.com