1 / 14

Repetition

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. Designing a loop block. Initialization

Download Presentation

Repetition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • Java while (Test) { Body } Test? true false Body

  5. int count; int sum count = 1; sum = 0; while ( count <= n ) { sum = sum + count; count = count + 1; } // print sum here Sum from 1 to N in Java Count  1 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 <= 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 (<initialization>; <test_condition>; <increment>) { // 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 <= n ) { sum = sum + count; count = count + 1; } // print sum here int count; int sum sum = 0; for ( count = 1; count <= 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 >= 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 <= 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( n*sumSq – sum*sum ) / ( 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 >= 1; temp-- ) for ( i = 1; i <= n; i++ )

More Related