Presentation is loading. Please wait.

Presentation is loading. Please wait.

Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -

Similar presentations


Presentation on theme: "Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -"— Presentation transcript:

1 do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop - Selection within a loop - Evaluating functions of one variable - Interactive loop control * Exercise/Home Work Dr. Ming Zhang

2 General Form of do/while Statement do statement(s); while (condition expression); The process continues until the condition expression evaluates to zero ( false) Dr. Ming Zhang

3 Flowchart of do/while Structure Enter do/while statement Execute the statement(s) after do exit the Loop false do Evaluate the statement expression true Dr. Ming Zhang

4 Example of do/while #include using std::cout; int main( ) { int counter = 1; do { cout << counter << “ “; } while (++counter <=10); return(0); } output: 1 2 3 4 5 6 7 8 9 10 Dr. Ming Zhang

5 break Statement * The break statement, when executed in a while, for, do/while, or switch structure, causes immediate exit for that structure. * Program execution continues with the first statement after the structure. * Common uses of the break statement are to escape early from a loop, or to skip the remainder of a switch structure Dr. Ming Zhang

6 Example of break Statement #include using std::cout; int main( ) { int x; for ( x=1; x<=10; x++) { if ( x ==5) break; //broke out of loop at x of 5 cout << x << “ “; } return(0); } Output: 1 2 3 4 Dr. Ming Zhang

7 Continue Statement * The continue statement, when executed in a while, for, or do/while structure, skips the remaining statements in the body of that structure, and proceeds with the next iteration of the loop. * In while and do/while structures, the loop- continuation test is evaluated immediately after the continue statement is executed. * In the for structure, the increment expression is executed, then the loop-continuation test is evaluated. Dr. Ming Zhang

8 Example of continue Statement #include using std::cout; int main( ) { int x=1; while ( x <=10) { if ( x ==5) //skip remaining code in continue; //loop only if x is 5 cout << x << “ “; x=x+1; } return(0);} Output: ??????????????????? Dr. Ming Zhang

9 Example of continue Statement #include using std::cout; int main( ) { int x=1; while ( x <=10) { if ( x ==5) //skip remaining code in continue; //loop only if x is 5 cout << x << “ “; x=x+1; } return(0);} Output: 1 2 3 4 …… infinite loop Dr. Ming Zhang

10 Example of continue Statement #include using std::cout; int main( ) { int x=0; while ( ++x <=10) { if ( x ==5) //skip remaining code in continue; //loop only if x is 5 cout << x << “ “; } return(0);} Output: 1 2 3 4 6 7 8 9 10 Dr. Ming Zhang

11 Example of continue Statement #include using std::cout; int main( ) { int x; for ( x=1; x<=10; x++) { if ( x ==5) //skip remaining code in continue; //loop only if x is 5 cout << x << “ “; } return(0); } Output: ???????????????? Dr. Ming Zhang

12 Example of continue Statement #include using std::cout; int main( ) { int x; for ( x=1; x<=10; x++) { if ( x ==5) //skip remaining code in continue; //loop only if x is 5 cout << x << “ “; } return(0); } Output: 1 2 3 4 6 7 8 9 10 Dr. Ming Zhang

13 Interactive Input within a Loop #include #define MAX 5 using std::cout; using std::cin; int main( ) {int count, num; float total, average; for(total=0.0,count=1;count<=MAX;++count) { cout << “Enter a number:”; cin >> num; total = total + num;} average = total/MAX; cout << “ The average is” << average<<endl; return(0);} Dr. Ming Zhang

14 Selection within a Loop #include #define MAX 5 #define THR 8 using std::cout; using std::cin; int main( ) {int count, num; float total, average; for(total=0.0,count=1;count<=MAX;++count) { cout > num; if(num> THR) total = total + num; else total = total - num;} average = total/MAX; cout << “ The average is” << average<<endl; return(0);} Dr. Ming Zhang

15 Evaluating Functions of One Variable # include using std::cout; using std::endl; int main( ) { int x, y; for (x=2; x<=6; ++x){ y =10*x*x +3*x -2; //function of one variable cout << x << y<<endl; } return(0); } Dr. Ming Zhang

16 Interactive Loop Control #include using std::cout; using std::endl; int main( ) { int num, final; cout <<“Enter the final number for the table:” cin << final;//variable final used to control a loop for(num = 1; num <= final; ++num) cout << num << (num*num); return(0) } Dr. Ming Zhang

17 Common Programming Error (15_1) # include using std::cout; using std::endl; int main( ) { int x, y; for (x=2; x<=1; ++x){ y =10*x*x +3*x -2; cout << x << y<<endl;} return(0);} ERROR--------------------------------------------- Dr. Ming Zhang

18 Common Programming Error (15_2) # include using std::cout; using std::endl; int main( ) { int x, y; for (x=2, x<=1, ++x){ y =10*x*x +3*x -2; cout << x << y<<endl;} return(0);} ERROR--------------------------------------------- Dr. Ming Zhang

19 Common Programming Error (15_3) # include using std::cout; using std::endl; int main( ) { int x, y; for (x=2;x<=1; ++x); { y =10*x*x +3*x -2; cout << x << y<<endl;} return(0);} ERROR--------------------------------------------- Dr. Ming Zhang

20 Common Programming Error (15_4) int num1 = 10, num2 = 10; cin >> opselect ; switch (opselect) { case 1: cout << “The sum:”<< (num1+num2); case 2: cout<<“The product”<<(num1*num2); break; case 3: cout<<“The division”<<(num1/num2); break; }................. ERROR--------------------------------------------- Dr. Ming Zhang

21 Common Programming Error (15_5) int num1 = 10, num2 = 10; cin >> opselect ; switch (opselect) { case1: cout << “The sum:”<< (num1+num2); break; case2: cout<<“The product”<<(num1*num2); break; case3: cout<<“The division”<<(num1/num2); break; }................. ERROR--------------------------------------------- Dr. Ming Zhang

22 Common Programming Error (15_6) int num1 = 10, num2 = 10; cin >> opselect ; switch (opselect) { case 1: cout << “The sum:”<< (num1+num2); break; case 1: cout<<“The product”<<(num1*num2); break; case 1: cout<<“The division”<<(num1/num2); break; }................. ERROR--------------------------------------------- Dr. Ming Zhang


Download ppt "Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -"

Similar presentations


Ads by Google