Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 2

3 3 The while Loop The general form of the while statement is: while(expression) { statement1; statement2; … }

4 4 The for Loop The general form of the for statement is: for(initial statement; loop condition; update statement) statement; for(initial statement; loop condition; update statement) {statement1; statement2; … } The for loop executes as follows: initial statement executes (initial statement initializes a variable) loop condition is evaluated If loop condition evaluates to true Execute for loop statement Execute update statement Repeat previous step until the loop condition evaluates to false

5 5 Example #include using namespace std; int main() { int i; for (i=0; i<10; i++) cout<<i<<" "; cout<<endl; return(0); }

6 6

7 7 The for Loop (continued) 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 In this case, the action of the for loop is empty If the loop condition is omitted It is assumed to be true

8 8 The for Loop (continued) 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;

9 9 The do … while Loop The general form of a do...while statement is: The procedure is: 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 do statement While(expression); do { statement1; statement2; … } While(expression);

10 10

11 11 Example for do…while loop #include using namespace std; int main() { int i=0; do { cout<<i<<" "; i=i+5; } while (i<=20); cout<<endl; return(0); }

12 12 Break & Continue Statements break and continue alter the flow of control break statement: syntax: break; The break statement is used for two purposes: 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) After the break statement executes, the program continues with the first statement after the structure The use of a break statement in a loop can eliminate the use of certain (flag) variables

13 13 #include 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); } Example for break statement

14 14 Break & Continue Statements (continued) continue statement Syntax: continue; used in a loop (while, for, and do-while structures) Skips remaining statements and proceeds with the next iteration of the loop In a while and do-while structure Expression (loop-continue test) is evaluated immediately after the continue statement In a for structure, the update statement is executed after the continue statement Then the loop condition executes

15 15 #include 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); } Example for continue statement

16 16 Nested Control Structures 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++) The value of i in the first iteration is 1, in the second iteration it is 2, and so on

17 17 Nested Control Structures (continued) The syntax is: for(i = 1; i <= 5 ; i++) { for(j = 1; j <= i; j++) cout<<"*"; cout<<endl; }

18 18 Nested Control Structures (continued) What pattern does the code produce if we replace the first for statement with the following? for (i = 5; i >= 1; i--) Our program becomes: for(i = 1; i <= 5 ; i++) { for(j = 1; j <= i; j++) cout<<"*"; cout<<endl; }

19 19 Answer: ***** **** *** ** *

20 20 End of lecture 11 Thank you!


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

Similar presentations


Ads by Google