1 / 5

CS 1400

CS 1400. Chapter 5, section 2. Loops!. while ( rel-expression ) while ( rel-expression ) { statements statement }. if the relational-expression is true, execute the statement(s) then repeat. (This is a pre-test loop). Example.

zarola
Download Presentation

CS 1400

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 1400 Chapter 5, section 2

  2. Loops! while (rel-expression) while (rel-expression) { statements statement } if the relational-expression is true, execute the statement(s) then repeat. (This is a pre-test loop)

  3. Example • Convert a list of temperatures from Fahrenheit to Centigrade. • int main() • { float fahr; • char continue = ‘Y’; • while (continue == ‘Y’) • { cout << “Enter a temperature: “; • cin >> fahr; • cent = 5.0/9.0 * (fahr-32.0); • cout >> “centrigrade conversion is: “ << cent << endl; • cout >> “Enter Y to continue…”; • cin >> continue; • } • }

  4. Counting loops • Convert exactly 5 temperatures to centigrade… • int main() • { float fahr; • int count = 0; • while (count < 5) • { cout << “Enter a temperature: “; • cin >> fahr; • cent = 5.0/9.0 * (fahr-32.0); • cout >> “centrigrade conversion is: “ << cent << endl; • count = count + 1; • } • }

  5. Examples… • Write a program to output the sum of a list of positive numbers provided by the user. • Modify the above program to output the average of a list of positive numbers provided by the user. • Write a program that prompts the user to enter an age in the range 18 to 25. Re-prompt the user if an invalid age is entered.

More Related