Lecture 11 Control Structures II Repetitioncont Introduction to




















- Slides: 20
Lecture 11: Control Structures II (Repetition)(cont. ) Introduction to Computer Science Spring 2006 1
2
The while Loop n The general form of the while statement is: while(expression) { statement 1; statement 2; … } 3
The for Loop n The general form of the for statement is: for(initial statement; loop condition; update statement) statement; for(initial statement; loop condition; update statement) { statement 1; statement 2; … } n The for loop executes as follows: n n initial statement executes (initial statement initializes a variable) loop condition is evaluated n If loop condition evaluates to true n Execute for loop statement n Execute update statement n Repeat previous step until the loop condition evaluates to false 4
Example #include <iostream> using namespace std; int main() { int i; for (i=0; i<10; i++) cout<<i<<" "; cout<<endl; } return(0); 5
6
The for Loop (continued) n n n initial statement in the for loop is the first to be executed and is executed only once If the loop condition is initially false, the loop body does not execute The update expression changes the value of the loop control variable which eventually sets the value of the loop condition to false The for loop executes indefinitely if the loop condition is always true A semicolon at the end of the for statement is a semantic error n n In this case, the action of the for loop is empty If the loop condition is omitted n It is assumed to be true 7
The for Loop (continued) n n In a for statement, all three statements (initial statement, loop condition, and update statement) can be omitted The following is a legal for loop: for(; ; ) cout<<"Hello"<<endl; 8
The do…while Loop n The general form of a do. . . while statement is: do statement While(expression); do { statement 1; statement 2; … } While(expression); n The procedure is: n n n statement executes first, and then the expression is evaluated If the expression evaluates to true, the statement executes again As long as the expression in a do. . . while statement is true, the statement executes 9
10
Example for do…while loop #include <iostream> using namespace std; int main() { int i=0; do { cout<<i<<" "; i=i+5; } while (i<=20); } cout<<endl; return(0); 11
Break & Continue Statements n break and continue alter the flow of control n break statement: n syntax: n The break statement is used for two purposes: n n break; In a repetition structure (e. g. while, for, and do…while loop), it immediately exits from a loop In a switch…case structure, skip the remainder of the switch structure (an immediate exit) n After the break statement executes, the program continues with the first statement after the structure n The use of a break statement in a loop can eliminate the use of certain (flag) variables 12
Example for break statement #include <iostream> using namespace std; int main() { int num, sum = 0; cin >> num; while(cin) { if (num<0) { cout<<"Negative number found!"<<endl; break; } sum=sum+num; cin>>num; } } cout<<"The sum is "<<sum<<endl; return(0); 13
Break & Continue Statements (continued) n continue statement n Syntax: continue; n used in a loop (while, for, and do-while structures) n n Skips remaining statements and proceeds with the next iteration of the loop In a while and do-while structure n n Expression (loop-continue test) is evaluated immediately after the continue statement In a for structure, the update statement is executed after the continue statement n Then the loop condition executes 14
Example for continue statement #include <iostream> using namespace std; int main() { int num, sum = 0; cin >> num; while(cin) { if (num<0) { cout<<"Negative number found!"<<endl; continue; } sum=sum+num; cin>>num; } } cout<<"The sum is "<<sum<<endl; return(0); 15
Nested Control Structures n n n Suppose we want to create the following pattern * ** ***** In the first line, we want to print one star, in the second line two stars and so on Since five lines are to be printed, we start with the following for statement for(i = 1; i <= 5 ; i++) n The value of i in the first iteration is 1, in the second iteration it is 2, and so on 16
Nested Control Structures (continued) n The syntax is: for(i = 1; i <= 5 ; i++) { for(j = 1; j <= i; j++) cout<<"*"; cout<<endl; } 17
Nested Control Structures (continued) n What pattern does the code produce if we replace the first for statement with the following? for (i = 5; i >= 1; i--) n Our program becomes: for(i = 1; i <= 5 ; i++) { for(j = 1; j <= i; j++) cout<<"*"; cout<<endl; } 18
n Answer: ***** *** ** * 19
End of lecture 11 Thank you! 20