150 likes | 172 Views
Learn about different types of loops in computer programming, including counter-controlled, sentinel-controlled, flag-controlled, and EOF-controlled while loops. Understand their syntax and application in solving problems.
E N D
Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006
The while Loop • The general form of the while statement is: while(expression) { statement1; statement2; … } • while is a reserved word • While Loop works in the following procedure: • Expression provides an entry condition • Statement executes if the expression initially evaluates to true • Loop condition is then reevaluated • Statement continues to execute until the expression is no longer true • Infinite loop: continues to execute endlessly
Counter-Controlled while Loops • If you know exactly how many pieces of data need to be read, the while loop becomes a counter-controlled loop • The syntax is: counter = 0; while(counter < N) { … counter++; … }
Counter-Controlled while Loops #include <iostream> using namespace std; int main() { int i; i=0; while (i<=20) { cout<<i<<" "; i=i+5; } cout<<endl; return(0); }
Sentinel-Controlled while Loops • Sentinel variable is tested in the condition and loop ends when sentinel is encountered • The syntax is: cin>>variable; while(variable != sentinel) { . cin>> variable; . }
Sentinel-Controlled while Loops #include <iostream> using namespace std; const int SENTINEL=-999; int main() { int number; cout << "If you want to terminate the program, please enter -999."<<endl; cout << "Enter a number: "; cin>>number; while (number != SENTINEL) { cout<<number<<" "<<endl; cout << "Enter a number: "; cin>>number; } cout<<endl; return(0); }
Flag-Controlled while Loops • A flag-controlled while loop uses a Boolean variable to control the loop • The flag-controlled while loop takes the form: found = false; while(!found) { … if(expression) found = true; … }
Flag-Controlled while Loops #include <iostream> using namespace std; int main() { bool found; int number; found=false; while (!found) { cout << "Enter a number: "; cin>>number; cout << "Your input is " <<number<<endl; if (number > 10) found=true; } cout<<endl; return(0); }
EOF-Controlled while Loops • Use an EOF (End Of File)-controlled while loop • The logical value returned by cin can determine if the program has ended input • The syntax is: cin >> variable; while (cin) { . cin >> variable; . }
EOF-Controlled while Loops #include <iostream> #include <string> using namespace std; int main() { int x; cin>>x; while(cin) { cout<<x; cin>>x; } return 0; }
The eof Function • The function eof can determine the end of file status • Like other I/O functions (get, ignore, peek), eof is a member of data type istream • The syntax for the function eof is: istreamVar.eof() where istreamVar is an input stream variable, such as cin
The eof Function #include <fstream> #include <iostream> using namespace std; int main() { ifstream infile; char ch; infile.open("c:\\tmp\\input.dat"); infile.get(ch); while (!infile.eof()) { cout << ch; infile.get(ch); } cout<<endl; return(0); }
End of lecture 10 Thank you!