Download presentation
Presentation is loading. Please wait.
Published byWendy Jordan Modified over 9 years ago
2
1 ELEC 206 Chapter 3 Control Structures
3
5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a hand example. 4. Develop a solution. 5. Test your solution. 2
4
Control structures Algorithm Development Conditional Expressions Selection Statements Repetition Statements Structuring Input Loops 3
5
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. 4
6
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. 5
7
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 6
8
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. 7
9
8 Structured Programming Sequence Selection Repetition ? truefalse ? true false ? => conditional expression
10
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. 9
11
10 Relational Operators ==equality ==equality !=non equality !=non equality <less than <less than >greater than >greater than <=less than equal to <=less than equal to >=greater than equal to >=greater than equal to
12
11 Logical Operators !not !not &&and &&and ||or ||or
13
Logical Operators ABA&&BA||B!A!B 000011 010110 100101 111100 12 Truth table for conditional expressions 0 = false 1=true
14
13 Operator Precedence 1. >= 2. == != 3. && 4. ||
15
14 (-6 =10) (3.0 >= 2.0) || (3.0 >= 4.0) (3.0 >= 2.0) && (3.0 >= 4.0) true && true results in true Practice! - evaluate true || false results in true true && false results in false
16
15 Selection Statements The C++ programming language supports the implementation of selection with: –if statements –switch statements
17
16 The if statement if(expression) statement; /*single statement executed if expression is true */ if expression is true */ // statement block is executed if expression is true. if(expression) { statement1; statement2; … statement n; }
18
17 The if statement - examples if (x>0) ++k; if(x>0) { x=sqrt(x); ++k; }
19
18 The if - else statement if(expression) statement; else statement; statement; if(expression) { statement block } else { statement block }
20
19 The nested if-else if(x > y) if(y < z) k++; else m++; else j++;
21
20 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?
22
21 The switch statement switch(expression) { case constant: statement(s); break; case constant: statement(s); break; /* default is optional*/ default: statement(s); }
23
22 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.
24
23 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;
25
24 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"; }
26
REPETITION STATEMENTS while statement do while statement for statement Structuring input loops 25
27
26 Repetition Statements The C++ programming language supports the implementation of repetition with: –while statements –do/while statements –for statements
28
27 The while statement while (expression) statement; { statement block } ? true false
29
28 The do/while statement dostatement; while (expression) do{ statement block } while (expression) ? true false
30
29 Practice ! #include 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?
31
30 The for statement initalize ? increment/ decrement true statement(s) false
32
31 The for statement for(initialization; expression; increment/decrement) statement; for(initialization; expression; increment/decrement) { statement; }
33
32 The for statement - examples //sum integers from //1 to 10 inclusive #include using namespace std; int main() { int sum=0; for(int i=1;i<11;++i) { sum = sum + i; } cout << sum << endl; return 0; } Alternate solution: //sum integers from //1 to 10 #include using namespace std; int main() { int sum=0; for(int i=1;i<=10;i++) sum = sum + i; cout << sum << endl; return 0; }
34
The for statement - example //sum odd integers from //1 to n inclusive #include<iostream> using namespace std; int main() { int sum=0, n; int sum=0, n; cout << "enter non-negative integer: "; cout << "enter non-negative integer: "; cin >> n; cin >> n; for(int i=1;i<=n;i+=2) for(int i=1;i<=n;i+=2) sum = sum + i sum = sum + i cout << sum << endl; cout << sum << endl; return 0; return 0;} 33
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; int sum=0, n; cout << "enter non-negative integer: "; cout << "enter non-negative integer: "; cin >> n; cin >> n; for(int i=1;i<=n;++i) for(int i=1;i<=n;++i) { if(i%2) sum = sum + i; if(i%2) sum = sum + i; } cout << sum << endl; cout << sum << endl; return 0; return 0;} 34
36
35 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.
37
36 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; }
38
37 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; }
39
38 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.
40
Structuring Input Loops 39 Repetition is useful when inputting data from standard input or from a file. Common repetition structures: counter-controlled sentinel-controlled end-of-data controlled
41
Counter-controlled Repetition Structure i 0 while i < = counter input data value //Do something with data value increment i end while 40
42
Sentinel-controlled Repetition Structure input data value while data value ! = sentinel value //Do something with data value input next data value end while 41
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 42
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.