Download presentation
Presentation is loading. Please wait.
Published byTheodora Caldwell Modified over 9 years ago
1
Control structures Algorithm Development Conditional Expressions Selection Statements 1
2
Algorithm Algorithm: a sequence of steps/actions needed to solve a problem. It is a set of sequential steps/actions that a C++ program should perform in order to solve the problem. These steps will eventually correspond to your C++ statements. –Top-down design of your Algorithm: – Start with a “big picture”, i.e., broad description of a problem solution in sequential step (i.e. order is important) –Then, refine the sequential steps until they are simple enough to translate to C++ language statements. The solution steps of your Top-Down design define your algorithm. 2
3
Algorithm Algorithm corresponds to Step 4 (Solution development) of the 5-Step Problem solving methodology How to describe your TOP-Down design (or algorithm)? Two techniques: –Pseudo Code: describes algorithm steps using English-like statements. –Flowchart: describes algorithm steps using a diagram/graphical notation
4
Top-Down Design – Example Problem : Increase salaries lower then 500 by 30 Omani Rials and display all salaries! 4 True False salary<500 Start main read salary salary=salary+30 Stop main print salary Program start/end symbol Input/output symbol Comparison symbol Computation symbol Program start/end symbol Input/output symbol Or NO Or Yes Pseudo CodeFlow Chart Broad Pseudo Code: Read salary value Increase salary values <500 by 30 Print salary Step 2 Needs Refinement: Refined Pseudo Code: Read salary value If ( salary <500) salary=salary+30 Print salary
5
Structured Programming – Sequence: Statements executed in order sequentially, one after another. This is the default structure in C++ programs –Selection: if, if-else, if-else if, switch One set of statements is executed if a given condition is true, a different set of statements is executed if the condition is false. –Repetition: while, do/while, for A set of statements is executed repeatedly as long as a given condition is true. 5
6
6 Structured Programming Sequence Selection Repetition ? truefalse ? true false ? => conditional expression Or Comparison
7
Conditional Expressions A conditional expression is a Boolean expression that evaluates to true or false. –Example: if (salary <500) salary=salary+30; Relational operators and logical operators are used to build conditional expressions. Selection structures and repetition structures use conditional expressions. 7
8
8 Relational Operators ==equal to !=not equal to <less than >greater than <=less than or equal >=greater than or equal
9
9 Relational ExpressionValue 1 == 6false 5 != 9true 5<9true 5>10false 2<=0false 2>=0true Practice – evaluate
10
10 Logical Operators !not &&and ||or - can link relational expressions - create complex conditional expressions Example Algebra: 90≤ mark ≤ 100 C++ Statement: mark >= 90 && mark <=100
11
Logical Operators ABA&&BA||B!A!B 000011 010110 100101 111100 11 Truth table for conditional expressions 0 = false 1= true
12
12 Operator Precedence 1.( ) 2.! 3. >= 4.== != 5.&& 6.||
13
13 (-6 =10) (3.0 >= 2.0) || (3.0 >= 4.0) (3.0 >= 2.0) && (3.0 >= 4.0) ! (3.0 >= 4.0) true && true results in true Evaluate Expressions true || false results in true true && false results in false ! false results in true
14
14 Selection Statements C++ selection statements: –if statements if statement if-else statement if-else-if statement –switch statements –? the conditional operator
15
15 The if statement one-way branch of action if(expression) statement; /*single statement executed if expression is true */ // statement block is executed if expression is true. if(expression) { statement1; statement2; … statement n; }
16
16 The if statement - examples if (salary<500) salary=salary+30; if(x>0) { x=sqrt(x); ++k; }
17
17 Nested if statement If statement (s) within an if statement block if(x>0) { if (y<100) x=sqrt(y); x+=4; }
18
18 The if - else statement 2-way branch of action if(expression) statement; else statement; if(expression) { statement block } else { statement block }
19
The if-else Statement - Example //determine grade result #include using namespace std; int main(){ double grade; cout<<“Enter grade: “; cin>>grade; if ( grade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "take this course again.\n"; } return 0; }
20
20 The nested if-else int x=9, y=7, z=2, k=0, m=0, j=0; if(x > y) if(y < z) k++; else m++; else j++;
21
21 The if – else if statement multi-way branch of action Form of nested if-else if(expression1) {statement;} else if(expression2) {statement;} else if(expression3) {statement;} …… else {statement;} if(expression1) {statement;} else { if(expression2) { statement;} else { if(expression3) { statement;} else { …….. } Each branch may contain a single statement or a block of statements encloses within braces:{ statement; statement; statement;}
22
22 The if – else if statement (Example1) If(day==1) cout<<“Saturday\n”; else if(day==2) cout<<“Sunday\n”; else if(day==3) cout<<“Monday\n”; else if(day==4) cout<<“Tuesday\n”; else if(day==5) cout<<“Wednesday\n”; else if(day==6) cout<<“Thursday\n”; else if(day==7) cout<<“Friday\n”; else cout<<“Sorry! Invalid day number\n.”;
23
23 The if – else if statement (Example2) If(grade>=90) cout<<“A”; else if(grade>=80) cout<<“B”; else if(grade>=70) cout<<“C”; else if(grade>=60) cout<<“D”; else cout<<“F”;
24
24 The if – else if statement (Example3) 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";
25
25 The switch statement switch(expression) { case constant: statement(s); break; case constant: statement(s); break; /* default is optional*/ default: statement(s); }
26
26 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.
27
27 switch statement example1 switch(grade)//grade of type char { case ‘A’: cout<<“Excellent Work!”<<endl; break; case ‘B’: cout<<“Good Work!”<<endl; break; case ‘C’: cout<<“Average Work!”<<endl; break; case ‘D’: case ‘F’: cout<<“Poor Work!”<<endl; break; }//end switch
28
28 switch statement example2 Switch (rank) { case 1: case 2: cout << "Lower division \n"; break; case 3: case 4: cout << “Upper division \n"; break; case 5: cout << “Graduate student \n"; break; default: cout << "Invalid rank \n"; }
29
29 The Conditional Operator The conditional operator replaces a simple if/else statement Syntax: expression ? statement1 : statement2; expression is a conditional expression. if expression is true statement1 is executed if expression is false statement 2 is executed Example: a>b? ++count : c=a+b; Equivalent if-else: if(a>b) ++count; else c=a+b;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.