1 / 15

Lecture 10:Control Structures II (Repetition)

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.

deines
Télécharger la présentation

Lecture 10:Control Structures II (Repetition)

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. Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006

  2. 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

  3. 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++; … }

  4. 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); }

  5. 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; . }

  6. 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); }

  7. 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; … }

  8. 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); }

  9. 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; . }

  10. 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; }

  11. 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

  12. 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); }

  13. End of lecture 10 Thank you!

More Related