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



Selection
Structures: the if Statement
General form of “if”
statement (version 1):
if (condition)
statement1;
else
statement2;
General form of “if”
statement (version 2):
if (condition)
statement1;
General form of “if”
statement (version 3):
if (condition) {
statement1;
statement2;
}
else {
statement2;
}
Important:
-
version 3 (also called if with compound
statements) is the most general form of if statement; it is usually used
when more than one statement follows “if” or “else”
-
version 1 is used when only one statement follows “if” and “else”
-
version 2 is used if “else” is not needed
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 considered 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. heart_rate
– it is TRUE if its value 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 (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 True Table:
|
x |
y |
x
&& y |
x
|| y |
!x |
|
nonzero
(TRUE) |
nonzero
(TRUE) |
TRUE
(1) |
TRUE
(1) |
FALSE
(0) |
|
nonzero
(TRUE) |
zero
(FALSE) |
FALSE
(0) |
TRUE
(1) |
FALSE
(0) |
|
zero
(FALSE) |
nonzero
(TRUE) |
FALSE
(0) |
TRUE
(1) |
TRUE
(1) |
|
zero
(FALSE) |
zero
(FALSE) |
FALSE
(0) |
FALSE
(0) |
TRUE
(1) |
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