Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program.

Similar presentations


Presentation on theme: "CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program."— Presentation transcript:

1 CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program Design in C, Sixth Edition, Pearson International Edition. Refer chapter 4 (Pg. 178 – 240) INSPIRING CREATIVE AND INNOVATIVE MINDS DDC1012: PROGRAMMING

2 SELECTION STRUCTURES: if and switch STATEMENTS 5.1Control Structures 5.2Conditions 5.3The if Statements 5.4If Statements with Compound Statements. 5.5Nested if Statements and Multiple-Alternative Decisions 5.6Decision Steps in Algorithms 5.7The switch Statement INSPIRING CREATIVE AND INNOVATIVE MINDS

3 Control structures – control the flow of execution in a program. Control structures - individual instructions into a single logical unit with one entry point and one exit point. Instruction are organized into three kinds of control structures to control execution flow: –Sequence –Selection –Repetition. Compound statement – a group of statements bracketed by { and } that are executed sequentially. INSPIRING CREATIVE AND INNOVATIVE MINDS 5.1 CONTROL STRUCTURES

4 Example: { statement1; statement2; … statementN; } A selection control structure chooses which alternative to execute. INSPIRING CREATIVE AND INNOVATIVE MINDS 5.1 CONTROL STRUCTURES

5 This topic already covered in chapter 4. You can refer this topic at page 179 - 190(Main TextBook). INSPIRING CREATIVE AND INNOVATIVE MINDS 5.2 CONDITIONS

6 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.3 THE if STATEMENTS if Statement with one alternative –Syntax as follows: If (condition) statements; –Example #1 if (x != 0.0) product = product * x; –Example #2 if (k < j) min = k; prinf(“Akhir kenyataan if\n”); will be executed if k < j Always be executed even k >= j

7 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.3 THE if STATEMENTS If statement with two alternatives – Syntax as follows: if (condition) statement; else statement; – Example if (rest_heart_rate > 56) printf(“Keep up your exercise!\n”); else printf(“Your heart is in execllent health!\n”);

8 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.4 if STATEMENTS WITH COMPOUND STATEMENTS Example #1 if (k < j) { min = k; prinf(“END if STATEMENT\n”); } Example #2 Source: This figure is taken from Pearson Addison-Wesley, 2009

9 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.4 if STATEMENTS WITH COMPOUND STATEMENTS Example #3 (Taken from Example 4.13 page 195) if (ctri < = MAX_SAFE_CTRI) { printf(“Car #%d: Safe\n”, auto_id); safe = safe + 1; } else { printf(“Car #%d: unsafe\n”, auto_id); unsafe = unsafe + 1; }

10 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Decision step – an algorithm step that selects one of several actions. Refer Case study : water Bill Problem (See page 198 – 206)

11 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.6 Structure Chart for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2009.

12 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.7 Program for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2009.

13 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.7 Program for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2009.

14 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.7 Program for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2009.

15 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.7 Program for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2009.

16 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.8 Sample Run of Water Bill Program Source: This figure is taken from Pearson Addison-Wesley, 2009.

17 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.6 NESTED if STATEMENTS AND MULTIPLE ALTERNATIVES DECISION  Nested if statements – an if statement with another if statement as its true task or its false task.  Example: if (x > 0) num_pos = num_pos + 1; else { if (x < 0) num_neg = num_neg + 1; else num_zero = num_zero + 1; }

18 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.6 NESTED if STATEMENTS AND MULTIPLE ALTERNATIVES DECISION  Multiple – Alternative Decision form of nested if: if (x > 0) num_pos = num_pos + 1; else if (x < 0) num_neg = num_neg + 1; else if num_zero = num_zero + 1;

19 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.6 NESTED if STATEMENTS AND MULTIPLE ALTERNATIVES DECISION  For more exercises, see - Example 4.16 at page 214 - 215 - Example 4.17 at page 215 - 216 - Example 4.18 at page 218 - Example 4.19 at page 218 - 220

20 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.7 THE switch STATEMENTS  The switch statement may also be used in C to select one of several alternatives.  Used expression in implementation. This expression also known as controlling expression  The value of this expression may be of type - char - int

21 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.7 THE switch STATEMENTS  Question taken from example 4.20 at page 222  Using switch statements to implement the following requirement: Class IDShipp Class B or bBattleship C or cCruiser D or dDestroyer F or fFrigate

22 INSPIRING CREATIVE AND INNOVATIVE MINDS 5.7 THE switch STATEMENTS Figure 4.12 Example of a switch Statement with Type char Case Labels Source: This figure is taken from Pearson Addison-Wesley, 2009.


Download ppt "CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program."

Similar presentations


Ads by Google