Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006.

Similar presentations


Presentation on theme: "1 Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006."— Presentation transcript:

1 1 Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006

2 2

3 3

4 4 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

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

6 6 Counter-Controlled while Loops #include using namespace std; int main() { int i; i=0; while (i<=20) { cout<<i<<" "; i=i+5; } cout<<endl; return(0); }

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

8 8 #include 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); } Sentinel-Controlled while Loops

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

10 10 #include 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); } Flag-Controlled while Loops

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

12 12 EOF-Controlled while Loops #include using namespace std; int main() { int x; cin>>x; while(cin) { cout<<x; cin>>x; } return 0; }

13 13 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

14 14 The eof Function #include 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); }

15 15 End of lecture 10 Thank you!


Download ppt "1 Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006."

Similar presentations


Ads by Google