1 / 15

C++ Programming Lecture 5 Control Structure I (Selection) – Part I

C++ Programming Lecture 5 Control Structure I (Selection) – Part I. By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department. Control Structures. Sequential execution Statements executed one after the other in the order written Transfer of control

johannat
Télécharger la présentation

C++ Programming Lecture 5 Control Structure I (Selection) – Part I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ ProgrammingLecture 5Control Structure I (Selection) – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

  2. Control Structures • Sequential execution • Statements executed one after the other in the order written • Transfer of control • When the next statement executed is not the next one in sequence • Bohm and Jacopini: all programs written in terms of 3 control structures (without any goto statement): • Sequence structure • Built into C++. Programs executed sequentially by default. • Selection structures • C++ has three types - if, if/else, and switch • Repetition structures • C++ has three types - while, do/while, and for The Hashemite University

  3. Combination of Control Structures • Two types of control structures combination: • Control structure stacking: use them one after the other. • Control structure nesting: use one control structure inside the body of another control structure. The Hashemite University

  4. The ifSelection Structure I • Selection structure • used to choose among alternative courses of action • Pseudocode example: If student’s grade is greater than or equal to 60 Display “Passed” • If the condition is true • print statement executed and program goes on to next statement • If the condition is false • print statement is ignored and the program goes onto the next statement • Indenting makes programs easier to read • C++ ignores white-space characters The Hashemite University

  5. The if Selection Structure II • Translation of pseudocode statement into C++: if ( grade >= 60 ) cout << "Passed"; • Diamond symbol (decision symbol) • indicates decision is to be made • Contains an expression that can be true or false. • Test the condition, follow appropriate path • if structure is a single-entry/single-exit structure The Hashemite University

  6. A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 istrue true false print “Passed” grade >= 60 The if Selection Structure III • Flowchart (or UML activity diagram) of pseudocode statement The Hashemite University

  7. The if/else Selection Structure I • if • Only performs an action if the condition is true (single selection structure) • if/else • A different action is performed when condition is true and when condition is false (double selection structure). • Psuedocode if student’s grade is greater than or equal to 60Display “Passed” else Display “Failed” • C++ code if ( grade >= 60 ) cout << "Passed";else cout << "Failed"; The Hashemite University

  8. grade >= 60 The if/else Selection Structure II • Ternary conditional operator (?:) (the only C++ ternary operator) • Takes three arguments (condition, value if true, value if false) • Our C++ code could be written as: cout << ( grade >= 60 ? “Passed” : “Failed” ); OR grade >= 60 ? cout << “Passed” : cout << “Failed”; • Remember that the precedence of ?: is low, so do not forget the parenthesis is used to force its priority. false true display “Failed” display “Passed” The Hashemite University

  9. Nestedif/elseStructure I • Nested if/else structures • Test for multiple cases by placing if/else selection structures inside if/else selection structures. if student’s grade is greater than or equal to 90 Print “A”else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F” • Once a condition is met, the rest of the statements are skipped • The above code can be rewritten using else if as in the next slide The Hashemite University

  10. Nestedif/elseStructure II if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F” • Pay attention to the indentation level and how it is differed from the previous slide. The Hashemite University

  11. The if/elseBody I • The body of if or else is the statement that will be implemented after if or else. • If no braces after if or else then the body will be only the first statement after them. • If there are braces, then the body will be the compound statement. The Hashemite University

  12. The if/elseBody II • Compound statement: • Set of statements within a pair of braces • Example: if ( grade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n";} • Without the braces, cout << "You must take this course again.\n"; would be automatically executed and the body of the else will be the first statement after it only. • Block of code • Compound statements with declarations The Hashemite University

  13. Note • While writing programs always use the following: • Using namespace std; • Not appending .h to the library name. • This is done to use the visual C++ libraries not the C libraries. The Hashemite University

  14. Examples • On board • Grading system. • Cars purchasing system. The Hashemite University

  15. Additional Notes • This lecture covers the following material from the textbook: • Fourth Edition: • Chapter 2: Sections 2.4, 2.5, 2.6 The Hashemite University

More Related