Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 : Selection Structures: if and switch statement By Suraya Alias.

Similar presentations


Presentation on theme: "Chapter 4 : Selection Structures: if and switch statement By Suraya Alias."— Presentation transcript:

1 Chapter 4 : Selection Structures: if and switch statement By Suraya Alias

2  Control structures Control the flow of execution in a program function. It enables you to combine individual instructions into a single logical unit with one entry point and one exit point. Instructions are organized into three kinds of control execution flow: 1.Sequence 2.Selection 3.Repetition  Compound Statement Is written as a group of statements bracketed by { and }; used to specify sequential flow. 1-2

3  Example of compound statement: { Statement 1; Statement 2;.. }  It controls flow from statement 1 to statement 2 and so on  Selection Control Structure A control structure that chooses among alternative program statements 1-3

4  An expression that is either false (represented by 0) or true (represented by 1) Example : rest_heart_rate > 75 Is true (1) if the value of rest_heart_rate is over 75 and false (0) if vice versa.  Relational and Equality Operator: 1-4 OperatorMeaningType <Less thanRelational >Greater thanRelational <=Less than or equal toRelational >=Greater than or equal toRelational ==Equal toEquality !=Not equal toequality

5  Unary Operator – an operator that has one operand  Binary Operators – an operator that has two operands  Logical Operators 1. && (and) 2. || (or) 3. ! (not)  We can form logical expression using logical operators  Example : 1. salary 5 This expression evaluates to 1 (true) if either the condition salary 5 is true 2. Temperature > 90.0 && humidity > 0.90 This expression evaluates to true only when BOTH conditions are true 1-5

6 Operand 1Operand 2Operand1 && Operand2 True/1 False/0 True/1False/0 Operand 1Operand 2Operand1 || Operand2 True/1 False/0True/1 False/0True/1 False/0 Operand 1!Operand 1 TrueFalse 01 1-6

7 1-7 Short circuit evaluation – stopping evaluation of a logical Expression as soon as the value can be determined

8 1-8 Test whether x lies within the range min through max (inclusive) The inclusive range is shaded. The expression is 1(true) if x lies within this range and 0 (false) If x is outside the range.

9 1-9 X and Y are greater than Z The shaded area represents the values of x that yields a TRUE result

10  If statement with TWO alternatives if (rest_heart_rate > 56) printf(“keep up your exercise program”) else Printf(“Your heart is in excellent health”) Form : if (condition) statement true; else statement false; 1- 10

11  If statement with ONE alternative if (x !=0.0) product = product * x; Form : if (condition) statement true; 1- 11

12 1- 12 Flowchart – a diagram that shows the step by step execution of a control structure

13 if ( money > 10.00) { printf(“lunch at McD”); balance = money–10.00; } else { Printf(“lunch at home”) balance = money; } if (condition){ True task } else { false task } 1- 13

14 1- 14 Statement PartXYtempEffect 12.55.0? If (x>y){12.5>5.0 is true temp = x;12.5Store old x in temp x = y;5.0Store old y in y y = temp;12.5Store old x in y Hand trace (desk check) – step by step simulation of an algorithm’s execution

15 1- 15

16  Decision steps – an algorithm step that selects one of several action. Coded in IF statements.  Pseudo-code – a combination of English phrases and C constructs to describe algorithm steps. Example : Algorithm for comp_late_charge Psuedocode:C program; If unpaid > 0if (unpaid > 0) Assess late charge late_charge = LATE_C; Elseelse Access no late charge late_charge = 0.0; return (late_charge)  Cohesive Function – a function that performs a single operation 1- 16

17 1- 17

18 1- 18

19 1- 19

20 1- 20

21 1- 21

22 1- 22

23 1- 23

24  Nested if statement An if statement with another if statement as its true task or its false task 1- 24 If (x>0) num_pos = num_pos + 1; Else if (x < 0) /*nested if*/ num_neg = num_neg + 1; else /*x equals 0*/ num_zero = num_zero + 1;

25 Syntax: If ( condition 1 ) statement 1 Else if ( condition 2 ) statement 2. Else if (condition n) statement n Else statement e 1- 25

26 Salary range ($)Base tax ($) Percentage of excess 0.00– 14,999.990.0015 15,000.00- 29,999.992,250.0018 30,000.00- 49,999.995,400.0022 50,000.00- 79,999.9911,000.0027 80,000.00- 150,000.0021,600.0033 1- 26

27 1- 27

28 1- 28

29 1- 29 Statement PartsalaryTaxEffect If (salary < 0.0) Else if (salary < 15000.0) Else if (salary < 30000.0) Tax = (salary -15000.00) * 0.18 + 2250.00 25000.00? 4050.00 25000.00 < 0 is false; 25000.00 < 15,000.0 is false; 25000.00 < 30,000.0 is true; Evaluates to 10000.00 Evaluates to 1800.00 Evaluates to 4050.00

30 1- 30

31 1- 31 If ( road_status == ‘S’ ) If ( temp > 0 ) { printf(“wet roads ahead\n”); printf(“stopping time doubled\n”); } else { printf(“icy roads ahead\n”); printf(“stopping time quadrupled\n”); } Else Printf(“Drive carefully\n”);

32  Can be used in C to select one of several alternative  Useful when the selection is based on the value of a single variable or of a simple expression (called the controlling expression)  The value of this expression may be type int or char but not double.  First the value in variable class is evaluated, then the list of case label is searched until it matches the value  Statement following the matching case label are executed until a break statement is encountered. 1- 32 Class IDShip class B or b C or c D or d F or f Battleship Cruiser Destroyer Frigate

33 1- 33 Syntax: switch(controlling expression) { Label set1 statement 1 break; Label set 2 statement 2 break; … Default: statement d }

34 1- 34


Download ppt "Chapter 4 : Selection Structures: if and switch statement By Suraya Alias."

Similar presentations


Ads by Google