Download presentation
Presentation is loading. Please wait.
Published byCarmella Hunter Modified over 8 years ago
1
© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements
2
Avoid infinite loops Can infinite loops ever be useful? Yes, but usually there is a safer way to do the same thing Should you purposely write and infinite loop? Generally not For some specific purposes, if you are very careful and know that the loop will be terminated regardless of the path taken through the program, an infinite loop may be useful. © Janice Regan, CMPT 128, 2013 1
3
Exiting a loop before it completes Sometimes you wish to exit from a loop even if the loop condition is still true For example if your loop is reading and processing data and an error occurs that can be recognized but cannot be corrected, you may wish to print an error message and exit the loop immediately How do you exit a loop while the loop condition is true? Use a break statement © Janice Regan, CMPT 128, 2013 2
4
3 The break Statement: break; A C++ statement that causes you to exit the loop you are in Program execution will continue at the statement following the loop
5
© Janice Regan, CMPT 128, 2013 4 break statement T F Condition Loop condition ….. Statement n; break; T F Statement 1; ….. condition2
6
© Janice Regan, CMPT 128, 2013 5 Example: break // Read and sum an unknown number of positive integers // Stop reading +ve integers when you read a -ve integer sum = 0; count = 0; while ( count < 10000000 ) // Safer than infinite loop { cin >> nextint; if( nextint < 0 ) break; sum += nextint; count++; } cout << sum;
7
Executing only some statements in a loop Sometimes you wish to execute a block of statements inside your loop only under particular conditions For example you may wish to skip the last half of the body of the loop if divisor = 0 (so you will not divide by 0) How do you execute only some of the statements inside the loop? Use a continue statement © Janice Regan, CMPT 128, 2013 6
8
7 The continue Statement: continue; A C++ statement that causes you to go from anywhere in the loop to the (update statement and loop condition test) Program execution will continue at the first statement inside the loop after the loop variable has been incremented and tested (if the test indicates another pass through the loop)
9
© Janice Regan, CMPT 128, 2013 8 continue statement T F Condition ….. Statement n; continue; T F Statement 1; ….. condition2
10
© Janice Regan, CMPT 128, 2013 9 Example: continue // Sum the even integers from 1 to 35 x = 0; sum = 0; while ( x <= 35 ) { x++; if ( x % 2 == 1 ) continue; sum += x; } cout << sum;
11
© Janice Regan, CMPT 128, 2013 10 Example: continue // Determine the kilometers per litre int litres = 0; int kilometers = 0; x = 0; while ( x < 1000 ) { cout << “enter #of liters and # kilometers”; cin >> litres >> kilometers ; x++; if ( litres == 0 ) continue; cout << “kilometers/liter “ << kilometers / litres; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.