Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Engineering Problem Solving with C++, Etter/Ingber Chapter 3 Control Structures Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Control structures Algorithm Development Conditional Expressions Selection Statements Repetition Statements Structuring Input Loops Engineering Problem Solving with C++, Second Edition, J. Ingber 2 Structured Programming Evaluation of Alternative Solutions ALGORITHM DEVELOPMENT Engineering Problem Solving with C++, Second Edition, J. Ingber 3 Algorithm Development An algorithm is a sequence of steps for solving a problem. Engineering problem solutions to real world problems require complex algorithms. Development of a good algorithm increases the quality and maintainability of a solution, and reduces the overall time required to implement a correct solution. Engineering Problem Solving with C++, Second Edition, J. Ingber 4 Top-Down Design Top-down design begins with a "big picture" description of a problem solution in sequential steps. The sequential steps are refined until the steps are detailed enough to translate to language statements. The refined steps, or algorithm, can be described using pseudo code or flowcharts. Engineering Problem Solving with C++, Second Edition, J. Ingber 5 Evaluation of Alternative Solutions Most problems have more than one solution. There may not be a single best solution, but some solutions are better than others. Elements that contribute to a good solution: – – – – – – – correctness reliability readability maintainability execution speed memory considerations user interface Engineering Problem Solving with C++, Second Edition, J. Ingber 6 Structured Programming A structured program is written using simple control structures, including: – Sequence – steps are performed one after another. – Selection – one set of statements is executed if a given condition is true, a different set of statements, or no statements at all, is executed if the condition is false. – Repetition –A set of statements is executed repeatedly as long as a given condition is true. Engineering Problem Solving with C++, Second Edition, J. Ingber 7 Structured Programming Sequence Selection Repetition false ? true false ? true Engineering Problem Solving with C++, Second Edition, J. Ingber ? => conditional expression 8 Relational operators Logical operators CONDITIONAL EXPRESSIONS Engineering Problem Solving with C++, Second Edition, J. Ingber 9 Conditional Expressions A conditional expression is a Boolean expression that evaluates to true or false. Selection structures and repetition structures rely on conditional expressions. Relational operators and logical operators are used to form conditional expressions. Engineering Problem Solving with C++, Second Edition, J. Ingber 10 Relational Operators == != < > <= >= equality non equality less than greater than less than equal to greater than equal to Engineering Problem Solving with C++, Second Edition, J. Ingber 11 Logical Operators ! && || not and or Engineering Problem Solving with C++, Second Edition, J. Ingber 12 Logical Operators A B A&&B A||B !A !B 0 0 0 0 1 1 0 1 0 1 1 0 1 0 0 1 0 1 1 1 1 1 0 0 Truth table for conditional expressions 0= Engineering Problem Solving with C++, Second Edition, J. Ingber 13 Operator Precedence 1. < <= > >= 2. == != 3. && 4. || Engineering Problem Solving with C++, Second Edition, J. Ingber 14 Practice! - evaluate (-6<0)&&(12>=10) true && true results in true (3.0 >= 2.0) || (3.0 >= 4.0) true || false results in true (3.0 >= 2.0) && (3.0 >= 4.0) true && false results in false Engineering Problem Solving with C++, Second Edition, J. Ingber 15 if statement switch statement SELECTION STATEMENTS Engineering Problem Solving with C++, Second Edition, J. Ingber 16 Selection Statements The C++ programming language supports the implementation of selection with: – if statements – switch statements Engineering Problem Solving with C++, Second Edition, J. Ingber 17 The if statement if(expression) statement; /*single statement executed if expression is true */ // statement block is executed if expression is true. if(expression) { statement1; statement2; … statement n; } Engineering Problem Solving with C++, Second Edition, J. Ingber 18 The if statement - examples if (x>0) ++k; if(x>0) { x=sqrt(x); ++k; } Engineering Problem Solving with C++, Second Edition, J. Ingber 19 The if - else statement if(expression) statement; else statement; if(expression) { statement block } else { statement block } Engineering Problem Solving with C++, Second Edition, J. Ingber 20 The nested if-else if(x > y) if(y < z) k++; else m++; else j++; Engineering Problem Solving with C++, Second Edition, J. Ingber 21 Practice! int x=9, y=7, z=2, k=0, m=0, j=0; if(x > y) if(y >z && y>k) k++; else m++; else j++; What are the values of j, k and m? Engineering Problem Solving with C++, Second Edition, J. Ingber 22 The switch statement switch(expression) { case constant: statement(s); break; case constant: statement(s); break; /* default is optional*/ default: statement(s); } Engineering Problem Solving with C++, Second Edition, J. Ingber 23 The switch statement Expression must be of type integer or character. The keyword case must be followed by a constant. break statement is required unless you want all subsequent statements to be executed. Engineering Problem Solving with C++, Second Edition, J. Ingber 24 switch statement example char ch; int ecount=0, vowels=0, other=0; cin.get(ch); while(!cin.eof()) { switch(ch) { case ‘e’: ecount++; case ‘a’: case ‘i’: case ‘o’: case ‘u’:vowels++; break; default: other++; }//end switch cin.get(ch); }//end while cout << ecount << ‘,’ << vowels << ‘,’ << other << endl; Engineering Problem Solving with C++, Second Edition, J. Ingber 25 Practice! Convert these nested if/else statements to a switch statement: if (rank==1 || rank==2) cout << "Lower division \n"; else { if (rank==3 || rank==4) cout << "Upper division \n"; else { if (rank==5) cout << "Graduate student \n"; else cout << "Invalid rank \n"; } } Engineering Problem Solving with C++, Second Edition, J. Ingber 26 while statement do while statement for statement Structuring input loops REPETITION STATEMENTS Engineering Problem Solving with C++, Second Edition, J. Ingber 27 Repetition Statements The C++ programming language supports the implementation of repetition with: – while statements – do/while statements – for statements Engineering Problem Solving with C++, Second Edition, J. Ingber 28 The while statement false ? while (expression) statement; true while (expression) { statement block } Engineering Problem Solving with C++, Second Edition, J. Ingber 29 The do/while statement do statement; while (expression) do { statement block } while (expression) true ? false Engineering Problem Solving with C++, Second Edition, J. Ingber 30 Practice! #include <iostream> using namespace std; int main() { int n=4; while(n>0) { cout << n << endl; --n; } cout << “value of n outside while is “ << n << endl; return 0; } Program Trace: Output? Engineering Problem Solving with C++, Second Edition, J. Ingber 31 The for statement initalize ? false increment/ decrement true statement(s) statement(s) Engineering Problem Solving with C++, Second Edition, J. Ingber 32 The for statement for(initialization; expression; increment/decrement) statement; for(initialization; expression; increment/decrement) { statement; statement; } Engineering Problem Solving with C++, Second Edition, J. Ingber 33 The for statement - examples Alternate solution: //sum integers from //sum integers from //1 to 10 inclusive //1 to 10 #include<iostream> #include<iostream> using namespace std; using namespace std; int main() int main() { { int sum=0; int sum=0; for(int i=1;i<11;++i) for(int i=1;i<=10;i++) { sum = sum + i; sum = sum + i; cout << sum << endl; } return 0; cout << sum << endl; } return 0; } Engineering Problem Solving with C++, Second Edition, J. Ingber 34 The for statement - example //sum odd integers from //1 to n inclusive #include<iostream> using namespace std; int main() { int sum=0, n; cout << "enter non-negative integer: "; cin >> n; for(int i=1;i<=n;i+=2) sum = sum + i cout << sum << endl; return 0; } Engineering Problem Solving with C++, Second Edition, J. Ingber 35 The for statement - example //sum odd integers from //1 to n inclusive //Alternate Solution #include<iostream> using namespace std; int main() { int sum=0, n; cout << "enter non-negative integer: "; cin >> n; for(int i=1;i<=n;++i) { if(i%2) sum = sum + i; } cout << sum << endl; return 0; } Engineering Problem Solving with C++, Second Edition, J. Ingber 36 Practice! Write a program solution to print all integer values between 1 and n that are multiples of 5 (ie evenly divisible by 5). Compare your solution with another person's solution. Engineering Problem Solving with C++, Second Edition, J. Ingber 37 The break statement break; – terminates loop – execution continues with the first statement following the loop Example: What is the output? for(int i-0; i<=10; ++i) { if(i%2) break; cout << i << endl; } Engineering Problem Solving with C++, Second Edition, J. Ingber 38 The continue statement continue; – forces next iteration of the loop, skipping any remaining statements in the loop Example: What is the output? for(int i-0; i<=10; ++i) { if(i%2) continue; cout << i << endl; } Engineering Problem Solving with C++, Second Edition, J. Ingber 39 Practice! //This while loop calculates n! int nfact=1, n; cout << "enter positive integer "; cin >> n; while(n > 1) { nfact = nfact*n; n--; } cout << n << "! = " << nfact << endl; //What is the output for n=5? //Write an alternate solution. Engineering Problem Solving with C++, Second Edition, J. Ingber 40 Structuring Input Loops Repetition is useful when inputting data from standard input or from a file. Common repetition structures: counter-controlled sentinel-controlled end-of-data controlled Engineering Problem Solving with C++, Second Edition, J. Ingber 41 Counter-controlled Repetition Structure i 0 while i < = counter input data value //Do something with data value increment i end while Engineering Problem Solving with C++, Second Edition, J. Ingber 42 Sentinel-controlled Repetition Structure input data value while data value ! = sentinel value //Do something with data value input next data value end while Engineering Problem Solving with C++, Second Edition, J. Ingber 43 eof()-controlled Repetition Structure input data value while end-of-file is not true //Do something with input data input next data value end while Engineering Problem Solving with C++, Second Edition, J. Ingber 44