1 / 20

Lecture 11:Control Structures II (Repetition) (cont.)

Lecture 11:Control Structures II (Repetition) (cont.). Introduction to Computer Science Spring 2006. The while Loop. The general form of the while statement is: while(expression) { statement1; statement2; … }. The for Loop. The general form of the for statement is :

whitlockd
Download Presentation

Lecture 11:Control Structures II (Repetition) (cont.)

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

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

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

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

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

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

  7. 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 { statement1; statement2; … } While(expression); do statement While(expression);

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

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

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

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

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

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

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

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

  16. Answer: ***** **** *** ** *

  17. End of lecture 11 Thank you!

More Related