Title: Selection
1Selection
2Structured Components
- There are three basic constructs that can be used
to build programs - Sequence
- One statement follows another.
- Selection
- Choose what statements to execute.
- Repetition
- Repeat statements.
3Selection In C
- If statement
- Many forms
- Switch statement
- Implements a case structure
4Compound Statement
- Groups a set of statements together under one
scope. - Used in selections.
statement1 statement2 statement3
5Function Body
- Uses a compound statement.
int MyFunction(int a, int b, int c) int
ReturnValue ReturnValue a b c return
ReturnValue
Compound Statement
6The if statement
if Condition do if true else do if false
- Condition is an expression that evaluates to
false (0) or true (?0). - If it evaluates to true, the first compound
statement is executed. - If it evaluates to false, the second compound
statement is executed.
7So, What Is A Condition?
- variable relational-operator variable
- a lt b
- variable relational-operator constant
- a gt 3
- variable equality-operator variable
- d e
- variable equality-operator constant
- fire ! 12
8Relational Operators
- lt less than
- lt less than or equal to
- gt greater than
- gt greater than or equal to
9Equality Operators
- Equal to.
- ! Not equal to.
- Be sure to remember both signs on the equal to
operator. - Be sure to review Table 4.2
- This author calls 1 true. Since 1 is ? 0, hes
right. But 2 is true, 3 is true, 129 is true.
Anything other than 0.
10Logical Expression
- rel_exress logical_operator rel_express
- Logical Operators
- And
- Or
- ! Not
- If ( a lt b c gt d )
- If a lt b and c gt d do true compound statement.
If either is false, do false compound statement.
11Logical Expression (cont.)
- if (e gt f gift snow)
- If either is true, execute true compound
statement. - If both are false, execute false compound
statement - if ! (cost lt 12.0)
- If cost is less than 12.0, execute false.
- If cost is greater than or equal to 12.0 execute
true.
12Watch comparing float to zero
- NEVER
- NEVER
- NEVER
- NEVER
- NEVER
- NEVER if ( float_variable 0.0 ) ..
13Did I say NEVER!!
- The reason is that internal representations for
float or double variables can have a bit set that
will fail a comparison to zero when the value is
basically zero. - Better to do the following
- if ( fabsf(float_variable) lt some_epsilon )
- Trust Me! (or trust someone else!) Just Trust!
- www.cygnus-software.com/papers/comparingfloats/com
paringfloats.htm
14Precedence?
- Study the book carefully.
- Or, just use parentheses.
15Short Circuit Evaluation
- If the first relational expression in an AND
logical expression is false, the rest of the
condition is not executed. The result is false. - If the first relational expression in an OR
logical expression is true, the rest of the
condition is not executed. The result is true.
16Example
- if (altb cgtd)
- If altb is false, cgtd is not evaluated.
- If (e ! f glth)
- If e ! f is true, glth is not evaluated.
- With relational, this isnt to significant. But
if the second relational also changes a value,
this can be very significant. (More Later)
17Character Relational
- Lexicographic (alphabetical) order is used.
- a gt c is false
- d lt f is true
18Logical Assignment
- The book shows using an int.
- Most people define a separate variable type bool
to make programs more readable. - result a lt b
- The comparison is made and the true or false is
stored in result. - if (result)
19DeMorgans Theorem
- To complement an expression, not both the
relationals and change the to . - To complement an expression, not both the
relationals and change the to . - !(altb cgtd) is the same as
- !(altb) !(cgtd)
20Dont Use Flow Charts
- They are cumbersome.
- They are old fashioned.
- They limit your thinking.
- Use pseudocode.
21IF ( ) THEN ELSE
if (a lt b) compound1 lt- If altb is true
execute this. else compound2 lt- If altb is
false execute this.
22One Statement ? No
If (fltg) statement1
if (dlte) statement1 else statement2
Null else branch!
If (hi) else statement2
Seldom used!
23Book Case Studies
- They have some nice case study examples.
- Dont lose the benefits by skipping over them too
lightly. - If you see something funny, bring it up in class.
24Nested If Statements
- Obviously, a compound statement may contain more
if statements. - Logic can be a bit tricky if they get too deep!
25Switch Statement
switch( size ) case 2 case 3
printf(small\n) break case 4
printf(medium\n) break
26Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
27Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
Output
28Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
3
4
5
6
2
Output
29Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
3
4
5
6
2
T
F
Output
30Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
3
4
5
6
2
T
F
F
4
Output
31Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
3
4
5
6
2
T
F
F
4
Output
32Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
False
3
4
5
6
2
T
F
F
4
Output
33Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
3
4
5
6
2
T
F
F
4
False
Output
34Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
3
4
5
6
2
T
F
F
4
True
Output
35Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
3
4
5
6
2
T
F
F
4
so long
Output
36Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
3
4
5
6
2
T
F
F
4
so long
Output
37Hand Trace
int main ( void ) int a3, b4, c5,
d6,total2 if( altb cgtd ) total
3 else total 4 switch (total)
case 2 printf(hello\n)
break case 3 printf(good-bye\n)
break default
printf(so long\n)
Given this code, what prints?
a
b
c
d
total
altb
cgtd
3
4
5
6
2
T
F
F
4
so long
Output
main exits