60 likes | 235 Vues
“Here we go Loop de Loo” (Looping). So far you have learned (I hope) some decision making C++ control structures: * if (condition) * if (condition) else * if (condition) else if (condition) … else * switch Now we will learn some ‘looping’ or repeating control structures.
E N D
“Here we go Loop de Loo” (Looping) So far you have learned (I hope) some decision making C++ control structures: * if (condition) * if (condition) else * if (condition) else if (condition) … else * switch Now we will learn some ‘looping’ or repeating control structures
First of Three Looping Control Structureswhile Loop while (continue_condition) { Statements to execute as long as continue_condition is true } Ex. int x, //loop control variable y; //used to output odd numbers less than 20 x = 0; //initialize because it must have a known //value prior to entering the loop while (x < 20) { y = x % 2; if (y == 1) cout << y << endl; x = x + 1; }
Reading Data from a FileUsing a Sentinel for End-of-Data One way to indicate the end of data in an input file is to place what is called a ‘sentinel’ value at the end of the data. When the program reads in the sentinel value, it will assume that all data has been gathered and stop accessing the input file for data. The ‘while’ loop is the best control structure to use to look for the sentinel value. Below is the pseudocode for the while structure. read first value from file while (value != sentinel) //Possibly while (value within range) process data read next value from file endwhile Note: Sometimes the ‘sentinel’ may not be a single value but a range. For example, the ‘sentinel’ may be any negative value.
Looking for the EOF (End-of-File) If there is no sentinel value then the program needs to look for the end of the file. When a file stream object gets data from a file it is assigned a value of true or false indicating whether or not the read was successful. This can be used to determine the EOF. Ex. intint_data; ifstream fin; fin.open(“input.txt”); while (fin >> int_data); { <Process int_data> }
while Loop Observations • The continue test condition is tested at the beginning of the loop (called pretest) • If the test condition tests false during the first iteration then the body of the while loop is NEVER executed • One must be careful that any loop control variable is initialized or acquires a valid value BEFORE the loop control structure is executed. • The test condition must get a new value at each iteration or you create an ‘infinite loop’ • The new value must at some point be able to terminate the loop execution
Suspect an Infinite Loop if … • Console output won’t stop • The program seems to go away but you can’t open the output file • Computer suddenly gets VERY slow • Stop button on Eclipse console panel stays RED. It should gray out once a program is done executing. To stop execution of an Eclipse program hit the RED stop button! Warning: An infinite loop writing to an output file can fill up the storage unit. This can cause file damage if this is your system drive. Note: All of you, at some time in your life, are extremely likely to write a program containing an infinite loop.