10doWhileFor_3014

10doWhileFor_3014 - Lecture File 10 COP 3014 January 9,...

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

View Full Document Right Arrow Icon
Lecture File 10 COP 3014 January 9, 2008 A. Ford Tyson 1 COP 3014 Lecture File 10 ± do while and for statements –syntax 1 – semantics – usage ± choosing an appropriate loop for a particular task Copyright 1997-present, Ann Ford Tyson do while loop ± SYNTAX do <statement> 2 while ( <expression> ); ± ACTION execute the statement once, then keep executing the statement as long as the expression is true loop body Flow of control: do while execute loop 3 body evaluate expression EXIT true false
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 10 COP 3014 January 9, 2008 A. Ford Tyson 2 do while characteristics p.1 ± what would happen if the expression was false when the loop was first 4 encountered? do while characteristics p.2 ± what would happen if the expression never becomes false ? 6 do while: Example Task: ask the user to enter a 'Y' for yes, or an 'N' for no. Keep asking until user gives a valid response. 8 Assume we have this declaration: char response;
Background image of page 2
Lecture File 10 COP 3014 January 9, 2008 A. Ford Tyson 3 cout << "Enter Y or N -> "; cin >> response; while ((response !='Y') && (response !='N')) review: while loop for this task 9 while ((response ! Y) && (response ! N)) { cout << "Please try again. Enter Y or N -> "; cin >> response; } do { cout << "Enter Y or N -> "; cin >> response functionally equivalent do while loop no priming read 10 cin >> response; if ((response != 'Y') && (response !='N')) cout << "Please try again. "; } while ((response != 'Y') && (response != 'N')); extra comparison test same expression as while do { cout << "Please Enter Y or N -> "; cin >> response better do while for this task: a more efficient version no extra comparison 11 cin >> response; } while ((response != 'Y') && (response != 'N')); can only print one prompt, but that is ok here
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 10 COP 3014 January 9, 2008 A. Ford Tyson 4 Interest Example ± Invest money in an account paying an annual rate of interest compounded monthly. How long, in months, until the 13 amount doubles? ± Example data: 1000.00 original amount 5.25 annual interest rate Interest Example Program p.1 #include <iostream> using namespace std; int main ( ) { const int MONTHS_PER_YEAR = 12, GOAL FACTOR = 2 14 GOAL_FACTOR = 2; int month;
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 / 14

10doWhileFor_3014 - Lecture File 10 COP 3014 January 9,...

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