140 likes | 238 Views
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
E N D
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 • 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.
Algorithm: Sum from 1 to N Count 1 Sum 0 Count ≤ N ? true Sum Sum + Count false Count Count + 1
Loops in Java • Java while (Test) { Body } Test? true false Body
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
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 }
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.
The FOR loop diagram Initialization Test condition? true false Body Increment
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
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; }
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;
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;
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 );
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++ )