201Spring08-L20

201Spring08-L20 - CMPSC 201C Spring 2008 Lecture 20...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 201C Spring 2008 Lecture 20 February 29, 2008 Common Uses of Loops Calculate the sum a series of numbers Calculate the product of a series of numbers Find the maximum of a series of numbers Find the minimum of a series of numbers Read lots of data from a file Finding Max/Min Finding the maximum number in a series if accomplished by a repeated comparison of the number to the current maximum value. If the number is greater than the maximum value, then the maximum value is reset to that number. There are two common ways to initialize the value of maximum: set it equal to the first value, or set it equal to an extremely low value. Finding Maximum // assuming you know how many numbers you will be // using (n) for (j = 0; j < n; j ++) { cout<<"Please enter a number\n"; cin>>number; // could be replaced by file input if ( j == 0 || number > max) { max = number; } } Finding Maximum max = -78688 // some unreasonably low number // assuming you know how many numbers you will be // using (n) for (j = 0; j < n; j ++) { cout<<"Please enter a number\n"; cin>>number; if (number > max) { max = number; } } Questions ???? Using "Break" in Loops You should never use break in loops in your homework for this course! (You will lose significant number of points!) Break is a way to interrupt, terminate loops. Break should be used very cautiously as it makes the code more difficult to understand. Break is usually part of an if structure if (cond) break; Most of the time break in loops can be avoided. Break Example Suppose your loop to find the sum should also terminate if the user enters a negative number without adding the negative number to the sum . for(j = 1; j <= n; j++) { cout<<"Please enter a number.\n"; cin>>number; if (number < 0) break; // terminates loop before sum is updated sum += number; } Rewrite Without Break for(j = 1; j <= n && number >= 0 ; j++) //number intialized { cout<<"Please enter a number.\n"; cin>>number; if (number >= 0) { sum += number; } } Sometimes you may want to change which loop structure is used. More on Break The break statement may be used to terminate the "infinite for loop" for ( ; ; ) { statement(s) if (cond) { break; } } Remember you are NOT to use either the break statement in a loopor the indefinite for loop in this course! (Breaks are often needed for switches) Break in Nested Loops When break is used in an inner loop, it only interrupts that loop, the iterations of outer loops would continue. When break is used in an outer loop, the inner loop is also ended. Questions ??? Using Continue in loops It is usually used with an if structure Tells the loop to skip over the remaining statements in the body and start the next iteration of the loop. Makes the logic of the program more difficult to understand. Use with caution, because the continue statement can cause infinite loops if the update is one of the statements that is skipped. Do not use continue for this course. Example of Continue Suppose you only want to sum the positive numbers that the user entered. You may be tempted to write the loop as follows.. for(j = 1; j <= n; j++) { cout<<"Please enter a number.\n"; cin>>number; if (number < 0) continue; // skips all remaining statements sum += number; } Example (cont) Could just as easily be written as... for(j = 1; j <= n; j++) { cout<<"Please enter a number.\n"; cin>>number; if (number >= 0) { sum += number; } } Note "Break" stops, or terminates, the loop. "Continue skips over statements for that specific iteration of the loop. Do not use either of these (or return out of a loop) Questions ...
View Full Document

This note was uploaded on 03/19/2008 for the course CMPSC 201 taught by Professor Susanquick during the Spring '08 term at Penn State.

Ask a homework question - tutors are online