# Ch2_7 - Next Previous Section 2.7 Do-While and For Loops Section 2.7 Do-While and For Loops 11 ® Do while loops ® For loops ® An example –

This preview shows pages 1–4. Sign up to view the full content.

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

View Full Document

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

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

Unformatted text preview: Next Previous Section 2.7: Do-While and For Loops Section 2.7: Do-While and For Loops 11 ® Do - while loops ® For loops ® An example – greenhouse gas concentration We will use both the assignment operator += and the unary operator ++ in this section. If you don’t remember what those do, go back to section 2.3, Slides 7 and 8. Review: a+=b; is the same as a=a+b; a++; is the same as a=a+1; Section 2.7: Do-While and For Loops Next Previous Section 2.7: Do-While and For Loops Section 2.7: Do-While and For Loops 22 ® do - while loops always execute once ® while loops do not have to execute once ® Syntax do { statements; statements; update criteria; } while (criteria check for ending loop); ® Simple example ® Sum a series of numbers ® Initial term in series is 0.0 ® Increment is 0.25 . 3 ... 75 . 50 . 25 . + + + + = y Do-while loops Next Previous Section 2.7: Do-While and For Loops Section 2.7: Do-While and For Loops 33 using namespace std; #include<iostream> int main() { double dx, xmax, x, y ; dx = 0.25 ; xmax = 3.0 ; y = 0.0 ; x= 0.0 ; Notice that we place more than one statement on a line. cout << "\n x = " << x ; cout << "\n \n y = " << y << endl <<endl ; system ( "pause" ); return ; } do { y += x ; x += dx ; } while (x <= xmax) ; The statements following the do are done once before the while criteria is checked. Here, the updated value of x is used as the loop criteria....
View Full Document

## This note was uploaded on 05/19/2008 for the course ENGR 25 taught by Professor All during the Spring '08 term at Lehigh University .

### Page1 / 15

Ch2_7 - Next Previous Section 2.7 Do-While and For Loops Section 2.7 Do-While and For Loops 11 ® Do while loops ® For loops ® An example –

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

View Full Document
Ask a homework question - tutors are online