Engineering Problem Solving with C++, Etter/Ingber Chapter 3 Control Structures Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber Control structures Algorithm Development Conditional Expressions Selection Statements Repetition Statements Structuring Input Loops Engineering Problem Solving with C++, Second Edition, J. Ingber
Algorithm Development Structured Programming Evaluation of Alternative Solutions Algorithm Development Engineering Problem Solving with C++, Second Edition, J. Ingber
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
Engineering Problem Solving with C++, Second Edition, J. Ingber 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
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
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
Structured Programming Sequence Selection Repetition ? false true ? true false ? => conditional expression Engineering Problem Solving with C++, Second Edition, J. Ingber
Conditional Expressions Relational operators Logical operators Conditional Expressions Engineering Problem Solving with C++, Second Edition, J. Ingber
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
Engineering Problem Solving with C++, Second Edition, J. Ingber 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
Engineering Problem Solving with C++, Second Edition, J. Ingber Logical Operators ! not && and || or Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber Logical Operators A B A&&B A||B !A !B 1 Truth table for conditional expressions 0 = Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber Operator Precedence < <= > >= == != && || Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber Practice! - evaluate (-6<0)&&(12>=10) (3.0 >= 2.0) || (3.0 >= 4.0) (3.0 >= 2.0) && (3.0 >= 4.0) true && true results in true true || false results in true true && false results in false Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber if statement switch statement Selection Statements Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber 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
Engineering Problem Solving with C++, Second Edition, J. Ingber 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
The if statement - examples if (x>0) ++k; if(x>0) { x=sqrt(x); } Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber The if - else statement if(expression) statement; else if(expression) { statement block } else Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber The nested if-else if(x > y) if(y < z) k++; else m++; j++; Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber 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++; j++; What are the values of j, k and m? Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber The switch statement switch(expression) { case constant: statement(s); break; /* default is optional*/ default: } Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber 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
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 }//end while cout << ecount << ‘,’ << vowels << ‘,’ << other << endl; Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber 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"; { if (rank==5) cout << "Graduate student \n"; cout << "Invalid rank \n"; } Engineering Problem Solving with C++, Second Edition, J. Ingber
Repetition Statements while statement do while statement for statement Structuring input loops Repetition Statements Engineering Problem Solving with C++, Second Edition, J. Ingber
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
Engineering Problem Solving with C++, Second Edition, J. Ingber The while statement ? true false while (expression) statement; { statement block } Engineering Problem Solving with C++, Second Edition, J. Ingber
The do/while statement ? true false do statement; while (expression) { statement block } while (expression) Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber 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
Engineering Problem Solving with C++, Second Edition, J. Ingber The for statement initalize ? false increment/ decrement true statement(s) statement(s) Engineering Problem Solving with C++, Second Edition, J. Ingber
Engineering Problem Solving with C++, Second Edition, J. Ingber The for statement for(initialization; expression; increment/decrement) statement; { } Engineering Problem Solving with C++, Second Edition, J. Ingber
The for statement - examples Alternate solution: //sum integers from //1 to 10 #include<iostream> using namespace std; int main() { int sum=0; for(int i=1;i<=10;i++) sum = sum + i; cout << sum << endl; return 0; } //sum integers from //1 to 10 inclusive #include<iostream> using namespace std; int main() { int sum=0; for(int i=1;i<11;++i) sum = sum + i; } cout << sum << endl; return 0; Engineering Problem Solving with C++, Second Edition, J. Ingber
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
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
Engineering Problem Solving with C++, Second Edition, J. Ingber 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
Engineering Problem Solving with C++, Second Edition, J. Ingber 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
The continue statement 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
Engineering Problem Solving with C++, Second Edition, J. Ingber 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
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
Counter-controlled Repetition Structure while i < = counter input data value //Do something with data value increment i end while Engineering Problem Solving with C++, Second Edition, J. Ingber
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
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