15brkContOpsRep_3014

15brkContOpsRep_3014 - Lecture File 15 COP 3014 July 9 2009...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture File 15 COP 3014 July 9, 2009 A. Ford Tyson 1 ± break and continue COP 3014 Lecture File 15 1 statements ± More C++ Operators ± Number Representation and Fundamental Errors Copyright 1997-present, Ann Ford Tyson break and continue statements ± STYLE NOTE : use of break and continue is generally to be avoided 2 – 1 entrance, 1 exit principle for control structures may be violated by their use – unconditional jumps often impair readability and can make flow of control confusing break and continue statements ± PERFORMANCE NOTE : judicious use of break and/or continue in certain situations may improve performance (efficiency, i.e. speed up the program) 3 and sometimes may even improve readability ± maximizing performance and maximizing readability can often be conflicting goals
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture File 15 COP 3014 July 9, 2009 A. Ford Tyson 2 in this course ± speed is not the primary concern, readability and good style habits are the primary concerns ± when is speed the primary concern? 4 REAL-TIME programming, for example, programs which control aircraft in flight, or a game which is played simulating "live" action ± in any case, all C++ programmers must know how these statements work from inside a loop, current loop body execution is terminated but the loop is not exited continue statement 5 ± while: jump to loop top and test expr ± do while: jump to loop bottom and test expr ± for: jump to update (expr3) What is the output ? for ( i = 1; i <= 5; i++ ) continue example p.1 6 { if ( i % 2 ) continue; else cout << i << endl; cout << "bottom of loop" << endl; }
Background image of page 2
Lecture File 15 COP 3014 July 9, 2009 A. Ford Tyson 3 What is the output ? for ( i = 1; i <= 5; i++ ) 2 "if odd number, continue, if even number, print it" continue example p.1a 7 for ( i 1; i 5; i ) { if ( i % 2 ) continue; else cout << i << endl; cout << "bottom of loop" << endl; } bottom of loop 4 bottom of loop exit innermost switch while do while or for break statement 8 exit innermost switch, while, do while or for statement What is the output ? for ( i = 1; i <= 5; i++ ) break example p.1 9 { if ( i % 2 ) break; else cout << i << endl; cout << "bottom of loop" << endl; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture File 15 COP 3014 July 9, 2009 A. Ford Tyson 4 What is the output ? for ( i = 1; i <= 5; i++ ) "if odd number, exit the loop, if even number, print it" break example p.1a 10 for ( i 1; i 5; i ) { if ( i % 2 ) break; else cout << i << endl; cout << "bottom of loop" << endl; } no output! Example: using continue // Ask user to enter 10 heights. Heights must be > 0 // Do not process any height which is <= 0 #include <iostream> using namespace std; const int NUM_PEOPLE = 10; 11 int main ( ) { int height, // a single height read maxHeight = 0, // initialize to impossible // value count = 1; // loop control variable Example: using continue p.2 while ( count <= NUM_PEOPLE ) { cout << "Enter a height in inches -> "; cin >> height;
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/14/2011 for the course COP 3014 taught by Professor Tyson during the Fall '10 term at FSU.

Page1 / 18

15brkContOpsRep_3014 - Lecture File 15 COP 3014 July 9 2009...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online