ICS103: Programming in C: 4: Selection Structures
ICS103: Programming in C: 4: Selection Structures
4: Selection Structures
Muhamed F. Mudawar
OUTLINE
Control Structures
Conditions, Relational, and Logic Operators
The if Statement and Flowchart
if with Compound Statements
Nested if statements
The switch Statement
Operator Precedence, Complementing a Condition
Common Programming Errors 2
CONTROL STRUCTURES
Control structure
Control the flow of execution in a program or a function
Repetition [Chapter 5]
}
CONDITIONS
Condition
Expression Value
'9' >= '0' 1 (true)
'a' < 'e' 1 (true)
'B' <= 'A' 0 (false)
'Z' == 'z' 0 (false)
'A' <= 'a' 1 (true)
ch >= 'a' && ch <= 'z' ch is lowercase? 10
ENGLISH CONDITIONS AS C
EXPRESSIONS
English Condition Logical Expression
x and y are greater than z x > z && y > z
x is equal to 1 or 3 x == 1 || x == 3
11
NEXT . . .
Control Structures
Conditions, Relational, and Logic Operators
The if Statement and Flowchart
if with Compound Statements
Nested if statements
The switch Statement
Operator Precedence, Complementing a Condition
Common Programming Errors 12
if STATEMENT (ONE ALTERNATIVE)
if (condition) statementT ;
Example:
if (x != 0.0)
product = product * x ; 13
if STATEMENT (TWO ALTERNATIVES)
if (condition) statementT ;
else statementF ;
if condition evaluates to true then statementT is
executed and statementF is skipped; Otherwise,
statementT is skipped and statementF is executed
Example:
if (x >= 0.0) printf("Positive"); 14
else printf("Negative");
FLOWCHARTS OF if STATEMENTS
Example
if (x > 0)
num_pos = num_pos + 1;
else
if (x < 0)
num_neg = num_neg + 1;
else /* x equals 0 */
19
num_zero = num_zero + 1;
MULTIPLE-ALTERNATIVE DECISION
FORM
The conditions are evaluated in sequence until a true
condition is reached
If a condition is true, the statement following it is
executed, and the rest is skipped
if (x > 0)
num_pos = num_pos + 1;
More
else if (x < 0)
Readable
num_neg = num_neg + 1;
else /* x equals 0 */ 20
num_zero = num_zero + 1;
SEQUENCE OF if STATEMENTS
All conditions are always tested (none is skipped)
Less efficient than nested if for alternative decisions
if (x > 0)
num_pos = num_pos + 1; Less
if (x < 0) Efficient
num_neg = num_neg + 1; than
if (x == 0) nested if
num_zero = num_zero + 1; 21
IMPLEMENTING A DECISION
TABLE
Use a multiple-alternative if statement to implement a
decision table that describes several alternatives
temperature
24
ROAD SIGN NESTED if STATEMENT
if (road_status == 'S')
if (temp > 0) {
printf("Wet roads ahead\n");
printf("Stopping time = 10 minutes\n");
} C associates else with the
else { most recent incomplete if
switch statement
Syntax is more readable
34
SHORT-CIRCUIT EVALUATION
Stopping the evaluation of a logical expression as soon as
its value can be determined
Logical-OR expression of the form (a || b)
If a is true then (a || b) must be true, regardless of b
No need to evaluate b
However, if a is false then we should evaluate b
In switch statements:
Make sure the controlling expression and case labels are of
the same permitted type (int or char)
Remember to include the default case