Sometimes we need to repeat a set of statements. - PowerPoint PPT Presentation

About This Presentation
Title:

Sometimes we need to repeat a set of statements.

Description:

Decrement = decrease by 1. Java includes special operators ... Decrement i: i--; It is recommended to use these operators ONLY in the following two situations: ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 16
Provided by: alanwi8
Category:

less

Transcript and Presenter's Notes

Title: Sometimes we need to repeat a set of statements.


1
Repetition
  • Sometimes we need to repeat a set of statements.
  • The body of the loop is itself a block (or set of
    blocks). It is repeated over and over until the
    test becomes false.

Test?
true
false
Loop Body
2
Designing a loop block
  • Initialization
  • Are there any variables to initialize for the
    test?
  • Test condition
  • A condition to determine whether or not to repeat
    the loop body
  • Loop body
  • What are the steps to repeat?
  • Do something that could potentially change the
    test condition to be false sometime in the
    future.

3
Algorithm Sum from 1 to N
Count ? 1
Sum ? 0
Count N ?
true
Sum ? Sum Count
false
Count ? Count 1
4
Loops in Java
Test?
true
Body
false
  • Java
  • while (Test)
  • Body

5
Sum from 1 to N in Java
Count ? 1
  • int count
  • int sum
  • count 1
  • sum 0
  • while ( count lt n )
  • sum sum count
  • count count 1
  • // print sum here

Sum ? 0
Count N ?
true
Sum ? Sum Count
false
Count ? Count 1
6
Infinite loops
  • If the test in a loop can never become false, the
    program will run forever without stopping.
  • In Dr. Java, to stop an infinite loop, click the
    Reset button.
  • Example
  • int count
  • int sum
  • count 1
  • sum 0
  • while ( count lt n )
  • sum sum count // forgot to update
    count

7
The FOR Loop
  • Java provides another format of a loop, which is
    usually used when we know how many times the loop
    body is to be executed.
  • The FOR loop has the following format
  • for (ltinitializationgt lttest_conditiongt
    ltincrementgt)
  • // body
  • In most cases, the initialization part
    initializes a counter, the test condition tests
    if the counter is within the limit, and the
    increment part modifies the counter.
  • Any FOR loop can always be formed as a WHILE loop
  • It does not give us any extra capability.
  • However, the notation is often more convenient.

8
The FOR loop diagram
Initialization
Test condition?
true
false
Body
Increment
9
Comparison of while and for
  • int count
  • int sum
  • count 1
  • sum 0
  • while ( count lt n )
  • sum sum count
  • count count 1
  • // print sum here
  • int count
  • int sum
  • sum 0
  • for ( count 1 count lt n count count 1 )
  • sum sum count
  • // print sum here

10
Example The factorial function
  • Definition of factorial function
  • Implemented in Java
  • int result
  • result n
  • for ( temp n-1 temp gt 1 temp temp - 1 )
  • result result temp

11
Example Mean and Standard Deviation
  • Arithmetic mean (average)
  • Standard deviation
  • As values of x are entered, keep the following
    sums
  • sum sum xi
  • sumSq sumSq xi xi

12
Mean and Standard Deviation in Java (1)
  • // Declare variables
  • int n // Number of data points
  • int i // Number of current data point
  • double xi // Current data point
  • double sum // Sum of data points
  • double sumSq // Sum of squares of data points
  • double mean // Arithmetic mean
  • double stDev // Standard deviation
  • // Read value of n
  • System.out.println("Enter number of values ")
  • n Keyboard.readInt( )
  • // Initialize sums
  • double sum 0.0
  • double sumSq 0.0

13
Mean and Standard Deviation in Java (2)
  • // Calculate sums
  • for ( i 1 i lt n i i 1 )
  • System.out.println("Enter x sub " i )
  • xi Keyboard.readDouble()
  • sum sum xi // sum
  • sumSq sumSq xi xi // sum of squares
  • // Calculate and print results
  • // Note that for both divisions, the numerator is
    a double
  • mean sum / n
  • stDev Math.sqrt( nsumSq sumsum ) / (
    n(n-1) )
  • System.out.println("The mean is " mean )
  • System.out.println("The standard deviation is "
    stDev )

14
Increment and Decrement operators
  • Increment increase by 1
  • Decrement decrease by 1
  • Java includes special operators for increment and
    decrement
  • Increment i i
  • Decrement i i--
  • It is recommended to use these operators ONLY in
    the following two situations
  • As single statements (as above)
  • In the increment area of a for loop.
  • for ( temp n-1 temp gt 1 temp-- )
  • for ( i 1 i lt n i )

15
Tracing
  • 1) int count
  • 2) int sum
  • 3) count 1
  • 4) sum 0
  • 5) while ( count lt 4 )
  • 6) sum sum count
  • 7) count count 1
  • 8) System.out.print(sum)

sum
count
Line
Write a Comment
User Comments (0)
About PowerShow.com