Chapter 4 : Selection Structures: if and switch statement By Suraya Alias
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
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
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
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
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
1-7 Short circuit evaluation – stopping evaluation of a logical Expression as soon as the value can be determined
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.
1-9 X and Y are greater than Z The shaded area represents the values of x that yields a TRUE result
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
If statement with ONE alternative if (x !=0.0) product = product * x; Form : if (condition) statement true; 1- 11
1- 12 Flowchart – a diagram that shows the step by step execution of a control structure
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
1- 14 Statement PartXYtempEffect ? 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
1- 15
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
1- 17
1- 18
1- 19
1- 20
1- 21
1- 22
1- 23
Nested if statement An if statement with another if statement as its true task or its false task 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;
Syntax: If ( condition 1 ) statement 1 Else if ( condition 2 ) statement 2. Else if (condition n) statement n Else statement e 1- 25
Salary range ($)Base tax ($) Percentage of excess 0.00– 14, , , , , , , , , , , , ,
1- 27
1- 28
1- 29 Statement PartsalaryTaxEffect If (salary < 0.0) Else if (salary < ) Else if (salary < ) Tax = (salary ) * ? < 0 is false; < 15,000.0 is false; < 30,000.0 is true; Evaluates to Evaluates to Evaluates to
1- 30
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”);
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 Class IDShip class B or b C or c D or d F or f Battleship Cruiser Destroyer Frigate
1- 33 Syntax: switch(controlling expression) { Label set1 statement 1 break; Label set 2 statement 2 break; … Default: statement d }
1- 34