Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS

Similar presentations


Presentation on theme: "CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS"— Presentation transcript:

1 CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
Learning outcomes Define the concept of selection structure. Apply the if, if/else, if/else if, switch/case selection structure to select actions. Design selection using algorithm for solving problems. Convert problem design into C code using selection controls. Trace an algorithm or program to verify that it does what user expect. Write a program using selection statements that control the flow of program execution Hanly, Koffman, C Program Design for Engineers, Second Edition, Addison Wesley, 2004. Reference... Prepared By: Pn. Nik Maria Nik Mahamood (Coordinator DDC 1012) Prepared by: Pn. Nik Maria Nik Mahamood

2 This chapter introduces to:
Control Structure Conditions The if Statements If Statements with Compound Statements. Nested if Statements and Multiple-Alternative Decisions Decision Steps in Algorithms The switch Statement

3 CONTROL STRUCTURES Control structure – a combination of 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 and repetition. Compound statement – a group of statements bracketed by { and } that are executed sequentially.

4 { statement1; } Example: statement2; … statementN;
A selection control structire chooses which alternative to execute.

5 CONDITIONS This topic already covered in chapter 4. You can refer this topic at page 147 – 157 (Main TextBook).

6 if (rest_heart_rate > 56)
THE if STATEMENT In C, the if statement is the primary selection control structure. If statement with two alternatives The if statement if (rest_heart_rate > 56) printf(“Keep up your exercise!\n”); else printf(“Your heart is in execllent health!\n”); Syntax as follows: if (ungkapan) kenyataan;

7 Figure 4.4 Flowcharts of if Statements with (a) Two Alternatives and (b) One Alternative Source: This figure is taken from Pearson Addison-Wesley, 2004.

8 if Statement with one alternative
if (x != 0.0) product = product * x; Syntax as follows: If (ungkapan) kenyataan; Another example if (k < j) min = k; prinf(“Akhir kenyataan if\n”); will be executed if k < j Always be executed even k >= j

9 IF STATEMENTS WITH COMPOUND STATEMENTS
Example #1 if (k < j) { min = k; prinf(“Akhir kenyataan if\n”); } Example #2 Both will be executed if k < j Source: This figure is taken from Pearson Addison-Wesley, 2004.

10 DECISISON STEPS IN ALGORITHMS
Decision step – an algorithm step that selects one of several actions. Case study : water Bill Problem See page

11 Figure 4.6 Structure Chart for Water Bill Problem
Source: This figure is taken from Pearson Addison-Wesley, 2004.

12 Figure 4.7 Program for Water Bill Problem
Source: This figure is taken from Pearson Addison-Wesley, 2004.

13 Figure 4.7 Program for Water Bill Problem (cont’d)
Source: This figure is taken from Pearson Addison-Wesley, 2004.

14 Figure 4.7 Program for Water Bill Problem (cont’d)
Source: This figure is taken from Pearson Addison-Wesley, 2004.

15 Figure 4.7 Program for Water Bill Problem (cont’d)
Source: This figure is taken from Pearson Addison-Wesley, 2004. Figure 4.8 Sample Run of Water Bill Program Source: This figure is taken from Pearson Addison-Wesley, 2004.

16 NESTED if STATEMENTS AND MULTIPLE-ALTERNATIVE DECISIONS
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; }

17 Comparison of nested if and sequence of if.
See Table 4.10 at page 180 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;

18 For more exercises, see example 4.16 at page 182 – 183
Example 4.19 at page 186 – 188 (Flowchart of example 4.19 shown as below) Source: This figure is taken from Pearson Addison-Wesley, 2004.

19 THE switch STATEMENT 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 But not of type double

20 Figure Example of a switch Statement with Type char Case Labels Source: This figure is taken from Pearson Addison-Wesley, 2004.


Download ppt "CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS"

Similar presentations


Ads by Google