Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.

Similar presentations


Presentation on theme: "Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement."— Presentation transcript:

1 Chapter 04 Control Statements: Part II

2 OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.

3 4.6 if…else Double-Selection Statement if…else –Performs one action if condition is true, a different action if it is false. Pseudocode –If student’s grade is greater than or equal to 60 print “Passed” Else print “Failed” C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

4 if...else double-selection statement activity diagram

5 Conditional Operator Conditional operator ( ?: ) –Three arguments (condition, value if true, value if false ) –Example: int Passed; if ( grade >= 60 ) Passed = 1; else Passed = 0; int Passed = (grade >= 60)? 1 : 0;

6 Conditional Operator Example: if (grade >= 60) cout << “Passed”; else cout << “Failed”; –The code can be written: cout = 60)? “Passed” : “Failed” ); ConditionValue if trueValue if false

7 4.6 if…else Double-Selection Statement Nested if…else statements –One inside another, test for multiple cases. –Once a condition met, other statements are skipped. –Example 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”

8 4.6 if…else Double-Selection Statement Nested if…else statements –Written In C++ if ( studentGrade >= 90 ) cout = 80 ) cout = 70 ) cout = 60 ) cout << "D"; else cout << "F";

9 4.6 if…else Double-Selection Statement The codes in the previous page can be written as: if ( studentGrade >= 90 ) { cout << "A"; } else { if (studentGrade >= 80 ) { cout << "B"; } else { if (studentGrade >= 70 ) { cout << "C"; } else { if ( studentGrade >= 60 ) cout << "D"; else cout << "F"; }

10 4.6 if…else Double-Selection Statement Nested if…else statements –Written In C++ (indented differently) if ( studentGrade >= 90 ) cout = 80 ) cout = 70 ) cout = 60 ) cout << "D"; else cout << "F";

11 Comparison

12 4.6 if…else Double-Selection Statement Compound statement using braces –Also called a block Set of statements within a pair of braces Used to include multiple statements in an if body –Example if ( studentGrade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } –Without braces, cout << "You must take this course again.\n"; always executes

13 4.6 if…else Double-Selection Statement Dangling- else problem –Compiler associates else with the immediately preceding if –Example if ( x > 5 ) if ( y > 5 ) cout 5"; else cout << "x is <= 5"; –Compiler interprets as if ( x > 5 ) if ( y > 5 ) cout 5"; else cout << "x is <= 5";

14 4.6 if…else Double-Selection Statement Dangling- else problem –Rewrite with braces ( {} ) if ( x > 5 ) { if ( y > 5 ) cout 5"; } else cout << "x is <= 5"; –Braces indicate that the second if statement is in the body of the first and the else is associated with the first if statement. –Suggestion: »While writing nested structure, use braces all the time.

15 Common Programming Error Forgetting one or both of the braces that delimit a block can lead to syntax errors or logic errors in a program. –Good Programming Practice To avoid omitting one or both of the braces, some programmers prefer to type the beginning and ending braces of blocks even before typing the individual statements within the braces.

16 4.6 if…else Double-Selection Statement Empty statement –A semicolon ( ; ) where a statement would normally be –Performs no action –Also called a null statement

17 Common Programming Error Placing a semicolon after the condition in an if statement leads to –a logic error in single-selection if statements, and –a syntax error in double-selection if...else statements. –Discuss if ( studentGrade >= 60 ); cout << "Passed.\n"; if (studentGrade >= 60); cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; }

18 4.7 while Repetition Statement Repetition statement –Action repeated while some condition remains true. –Pseudocode While there are less than 100 products, triple the amount of products. –while loop repeats until condition becomes false.

19 Fig. 4.6 | while repetition statement UML activity diagram.

20 4.7 while Repetition Statement Merge symbol –Joins two or more flows of activity into one flow of activity –Represented as a diamond Unlike the decision symbol a merge symbol has –Multiple incoming transition arrows –Only one outgoing transition arrows »No guard conditions on outgoing transition arrows

21 4.7 while Repetition Statement C++ Implementation: int product = 10; while ( product <= 100 ) { cout << “There are ” << product << “ items.” << endl; product = product * 3; } –Note: Indent the statement(s) in the body of an if statement to enhance readability. If there is only one statement in the body, the braces can be removed.

22 4.7 while Repetition Statement C++ Implementation: int product = 10; while ( product <= 100 ) product = product * 3; –Note: The variables used in the condition should be initialized. –Discuss the result when the variable is not initialized. The statements in the body will be written to change the condition so as to break loop.

23 Infinite Loop Example: –Note: This can make a program appear to “hang” or “freeze”. int product = 10; while (product > 0) { product = product * 3; } int product = 10; while (product > 0) ; int product = 10; while (1) { product = product * 3; …… }

24 Mixed Usage of if- and while- statements if- and while-statements could be mixed or nested. Indenting codes properly can increase readability of your program. while (option != -1) { if (…) { … } else if (…) { while (…) { … }

25 Indenting Functions in VS Press or to decrease/increase indenting. Decrease/Increase indenting


Download ppt "Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement."

Similar presentations


Ads by Google