Selection Structures (Chapter 4)
Control Structures
- There are 3 types of
control structures:



Selection
Structures: the if Statement
General form of “if”
statement (called if with compound
statements):
if (condition) {
statement1;
statement2;
}
else {
statement2;
}
Special form of “if”
statement (if only a single statement is executed after if and else):
if (condition)
statement1;
else
statement2;
Special form of “if”
statement (if only a single statement should be performed if condition is TRUE,
and no action is required if condition is FALSE):
if (condition)
statement1;
Example:
if (rest_heart_rate > 56)
printf(“Take some
exercise! \n”);
else
printf(“You
health is fine! \n”);
Reading Assignment:
-
Case Studies from sections 4.5 and 4.6
Nested “if” Statements:
if (road_status == ‘S’)
if (temp > 32)
printf(“Warning: wet road ahead! \n”);
else
printf(“Warning: icy road ahead” \n);
else
printf(“Drive
carefully! \n);
printf(“Hello everybody! \n);
Comments:
-
the first printf is executed only if value of road_status variable is ‘S’ and value
of temp is larger than 32
-
the second printf is executed only if value of road_status variable is ‘S’ and value
of temp is less or equal to 32
-
the third printf is executed only if value of road_status variable is not ‘S’
-
the fourth printf is executed always, since it is outside of the nested
“if” statements
Important:
-
“else” is associated with the most recent incomplete “if”
Using “if” to allow Multiple-Alternative Decisions
if (condition1)
statement1; (condition1
is TRUE)
else if (condition2)
statement2; (condition1
is FALSE and condition2 is TRUE)
else
statement3; (condition1
and condition2 are FALSE)
Conditions
- Condition is an expression that can be TRUE or FALSE (related with Boolean Algebra)
- Basic forms of condition expressions:
variable1
(e.g. tasty – it is TRUE if value of tasty
is nonzero, it is FALSE if its value is 0)
variable1 operator CONSTANT
(e.g. heart_rate > 56 – it is
TRUE if heart_rate value is larger than 56)
variable1 operator variable2
(e.g.
apples ==
oranges – it is TRUE if value of apples
is the same as value of oranges)
Relational and Equality Operators:
< (less
than) , > (large than) , <= (less
than or equal), >= (large than or equal), == (equal),
!= (not equal)
Logical Operators:
-
Allow creation of complicated conditions called logical expressions
- && (AND), || (OR), ! (NOT)
-
Example:
(x > 0 && x < 5)
means “x is larger
than 0 and smaller than 5”
-
Example:
(salary < MIN_SALARY || dependents > 5)
To understand logical expression, we should be familiar with the Truth Table:
|
x |
y |
x
&& y |
x
|| y |
!x |
|
nonzero
(TRUE) |
nonzero
(TRUE) |
1
(TRUE) |
1
(TRUE) |
0
(FALSE) |
|
nonzero
(TRUE) |
zero
(FALSE) |
0
(FALSE) |
1
(TRUE) |
0
(FALSE) |
|
zero
(FALSE) |
nonzero
(TRUE) |
0
(FALSE) |
1
(TRUE) |
1
(TRUE) |
|
zero
(FALSE) |
zero
(FALSE) |
0
(FALSE) |
0
(FALSE) |
1
(TRUE) |
Operator Precedence:
Ø
function calls
Ø
unary operators !, +, -, &
Ø
*,
/, %
Ø
binary operators +, -
Ø
<,
<=, >, >=
Ø
==,
!=
Ø
&&
Ø
||
Ø
+
Examples: assume x=3.0,
y=4.0, z=2.0, flag=0
x + y/z <= 3.5 (FALSE)
!flag || y + z >= x - z (TRUE)
! (flag || y + z >= x
- z) (FALSE)
Reading Assignment: Writing English Conditions in C (p. 152)
Logical Assignment
- We
can assign the outcome of a condition to a variable of int or double type
- Example:
senior_citizen
= (age >= 65);
Interpretation:
the condition in the parenthesis is TRUE if age is larger
or equal to 65; in this case senior_citizen
is assigned value 1; otherwise it is assigned value 0
Short-Circuit Evaluation of Logical Expressions
x && y (if x is zero x&&y is zero regardless of y)
x || y (if x is one x||y is one regardless of y)
DeMorgan’s Theorem
! (x && y) is the same as
!x || !y
! (x || y) is
the same as !x
&& !y
Common Programming
Errors (Section 4.9)
Example
1
if (0 <= x <= 4)
printf(“x is between 0 and
4 inclusive”);
Explanation: Assume x equals 10. The condition is evaluated
from left to right. Then, the value of 0 <= x is 1 (TRUE). Then, the value of 1
<=4 is 1 (TRUE), and the whole
condition 0 <= x <= 4 is evaluated as TRUE! The right way to write the
condition is (0 <= x && x <= 4).
Example
2
if(x = 10)
printf(“x is 10”);
Explanation: meaning of (x
= 10) is to
assign value 10 to x. The value of (x = 10) is 10, which is TRUE (remember, every nonzero value is
interpreted as TRUE). Therefore, “x is 10” is always printed,
regardless of the initial value of x. The right way to write the
condition is (x == 0).
Example
3
if(x > 0)
sum = sum + x;
printf(“Greater than zero”)
else
printf(“Less or equal to
zero”);
Explanation: We forgot to use braces
after if. So, after sum = sum + x is executed the computer will assume we are
out of if-else statement. Then, printf will be executed, and when else is encountered the computer
will be confused (since there is always if before else) and program will have a syntax
error.