Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.

Similar presentations


Presentation on theme: "C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook."— Presentation transcript:

1 C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)

2 The Hashemite University2 Outline Introduction. if statement. if/else statement. Nested if/else statements. Examples.

3 The Hashemite University3 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 All programs written in terms of 3 control structures: 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

4 The Hashemite University4 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.

5 The Hashemite University5 The if Selection 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

6 The Hashemite University6 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

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

8 The Hashemite University8 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 60 Display “Passed” else Display “Failed” C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

9 The Hashemite University9 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 = 60 ? “Passed” : “Failed” );//omitting ( ) is syntax error OR grade >= 60 ? cout << “Passed” : cout << “Failed”; Remember that the precedence of ?: is low, so do not forget the parenthesis that is used to force its priority. truefalse display “Failed”display “Passed” grade >= 60

10 The Hashemite University10 Nested if/else Structure 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 Display “A” else if student’s grade is greater than or equal to 80 Display“B” else if student’s grade is greater than or equal to 70 Display “C” else if student’s grade is greater than or equal to 60 Display “D” else Display“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

11 The Hashemite University11 Nested if/else Structure II if student’s grade is greater than or equal to 90 Display “A” else if student’s grade is greater than or equal to 80 Display “B” else if student’s grade is greater than or equal to 70 Display“C” else if student’s grade is greater than or equal to 60 Display “D” else Display“F” Pay attention to the indentation level and how it is differed from the previous slide.

12 The Hashemite University12 The if/else Body 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.

13 The Hashemite University13 The if/else Body 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

14 The Hashemite University14 The if/else Body III Every if/else or if is considered as one statement with their bodies. How?? Placing lines of codes between else and the body of its if is syntax error.  Leaving the parenthesis after the if empty (you have not put an expression for the condition) is syntax error.  if () // no condition  syntax error  cout<<"b"; If( grade<50) { cout<<“F”; cout<<“Fail”; } cout<<“you fail”; // syntax error else Cout<<“pass”; if (grade > 60) cout<<"b"; cout<<"hello"; // will produce a syntax error else cout<<"c";

15 The Hashemite University15 Examples On board Grading system

16 The Hashemite University16 Additional Notes This lecture covers the following material from the textbook: Fourth Edition: Chapter 2: Sections 2.4, 2.5, 2.6


Download ppt "C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook."

Similar presentations


Ads by Google