130 likes | 668 Views
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)
E N D
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) • DO - WHILE = 1 (post-loop condition)
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
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
FOR Loop • Syntax: for (initialization ; condition ; update ) { …… statements ……. } Example # 4
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