09while_3014

09while_3014 - Lecture File 09 COP 3014 January 9, 2008 COP...

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

View Full Document Right Arrow Icon
Lecture File 09 COP 3014 January 9, 2008 Instructor: A. Ford Tyson 1 COP 3014 Lecture File 09 ± Iteration and the while statement 1 – major loop types – major loop tasks –mac ros – simulation and pseudo-random numbers Copyright 1997-present, Ann Ford Tyson Iteration ± Iteration (looping): control structure which allows statements to execute repeatedly 2 ± Iteration statements in C++ – while – do while –for while loop ± most general, can implement any form of loop structure needed ± SYNTAX true or false 3 while ( <expression> ) <statement> ± ACTION while the expression is true, execute the statement loop body
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 09 COP 3014 January 9, 2008 Instructor: A. Ford Tyson 2 Flow of control: while evaluate false 4 expression execute loop body EXIT true while characteristics p.1 ± what would happen if the expression was false when the loop was first encountered? 5 while characteristics p.2 ± what would happen if the expression never became false ? 7
Background image of page 2
Lecture File 09 COP 3014 January 9, 2008 Instructor: A. Ford Tyson 3 infinite loop characteristics ± may see same output over and over ± may see computer seem to "hang" ± to stop (Windows PC, Visual ++): 9 – click on the usual X box – control-alt-delete – may need to restart Did you hear about the computer scientist found dead in the shower? Sure! They thought he had been dead for days 10 Sure! They thought he had been dead for days. And in his hand, he was clutching a shampoo bottle with these directions: lather rinse repeat Is that an algorithm? Overview of Major Loop Types loops count-controlled event-controlled 11 iteration counter sentinel flag EOF EOLN
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 09 COP 3014 January 9, 2008 Instructor: A. Ford Tyson 4 Overview of Major Loop Tasks loops counting keeping track of a 12 event counter summing running total keeping track of a previous value first loop example ± sentinel-controlled loop executes until a particular value is read ± event-controlled loop 13 (more general term) executes until some event occurs ± first example: sentinel-controlled, event- controlled, sentinel is 'Y' or 'N' while: sentinel-controlled // ask user for Y or N and recover from // data entry errors char response; cout << "Enter Y or N -> "; cin >> response // priming read 14 cin >> response; while ((response != 'Y') && (response != 'N')) { cout << "Please try again. Enter Y or N -> "; cin >> response; }
Background image of page 4
Lecture File 09 COP 3014 January 9, 2008 Instructor: A. Ford Tyson 5 How many times does the loop body execute? zero or as many as needed first example: questions 15 What is the value of response after the loop exits? complement of expr must be true, hence (response == 'Y') || (response == 'N') what if we changed the && to || ? first example: what if questions p.1 16 what if we forgot the priming read? first example: what if questions p.2 18
Background image of page 5

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

View Full DocumentRight Arrow Icon
Lecture File 09 COP 3014 January 9, 2008 Instructor: A. Ford Tyson 6 second loop example ± count-controlled loop executes a specified number of times a counter controls the number of iterations ± example: print the numbers from 1 through 20 100 and their squares, as in the next table ± notice in the code: – loop body executes 100 times
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 23

09while_3014 - Lecture File 09 COP 3014 January 9, 2008 COP...

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

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