1 / 7

WHILE Loop

WHILE Loop. Syntax: while ( condition ) // condition=true { ……statements …… }. Example #1. DO-WHILE Loop. Syntax: do { ……statements …… } while ( condition ) // condition=true. Example #2. What’s the Difference?. Minimum # of loops WHILE = 0 (pre-loop condition)

saburo
Download Presentation

WHILE Loop

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. WHILE Loop • Syntax: while (condition) // condition=true { ……statements …… } Example #1

  2. DO-WHILE Loop • Syntax: do { ……statements …… } while (condition) // condition=true Example #2

  3. What’s the Difference? • Minimum # of loops • WHILE = 0 (pre-loop condition) • DO - WHILE = 1 (post-loop condition)

  4. File Input/OutPut • To read and write data from and to a file is a five step process- 1.Include the header file fstream in the program #include <fstream> 2.Declare file stream variable ifstream indata; ofstream outdata; 3.Associate the file stream variable with the input/output sources. Indata.open(“a:\\prog.dat);//open the input file Outdat.open(“a:\\prog.out);//open the output file

  5. 4.Use the file stream variables with <<,>> or other output/input functions to read or write data into a file. indata>>payrate; outdata<<“the paycheck is: $”<<pay; 5.Close the file Indata.close(); Outdata.close(); EXAMPLE # 3

  6. FOR Loop • Syntax: for (initialization ; condition ; update ) { …… statements ……. } Example # 4

  7. When to Use what? • Do you know the number of repetitions? • Prefer for loop – definite. • Do you have uniform increments? • Prefer for loop • At least once? • Do-While loop

More Related