Selection - PowerPoint PPT Presentation

About This Presentation
Title:

Selection

Description:

Implements a case structure. Compound Statement ... The book shows using an int. ... Book Case Studies. They have some nice case study examples. ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 38
Provided by: csMon
Category:
Tags: selection

less

Transcript and Presenter's Notes

Title: Selection


1
Selection
2
Structured 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.

3
Selection In C
  • If statement
  • Many forms
  • Switch statement
  • Implements a case structure

4
Compound Statement
  • Groups a set of statements together under one
    scope.
  • Used in selections.

statement1 statement2 statement3
5
Function Body
  • Uses a compound statement.

int MyFunction(int a, int b, int c) int
ReturnValue ReturnValue a b c return
ReturnValue
Compound Statement
6
The 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.

7
So, 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

8
Relational Operators
  • lt less than
  • lt less than or equal to
  • gt greater than
  • gt greater than or equal to

9
Equality 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.

10
Logical 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.

11
Logical 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.

12
Watch comparing float to zero
  • NEVER
  • NEVER
  • NEVER
  • NEVER
  • NEVER
  • NEVER if ( float_variable 0.0 ) ..

13
Did 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

14
Precedence?
  • Study the book carefully.
  • Or, just use parentheses.

15
Short 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.

16
Example
  • 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)

17
Character Relational
  • Lexicographic (alphabetical) order is used.
  • a gt c is false
  • d lt f is true

18
Logical 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)

19
DeMorgans 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)

20
Dont Use Flow Charts
  • They are cumbersome.
  • They are old fashioned.
  • They limit your thinking.
  • Use pseudocode.

21
IF ( ) THEN ELSE
if (a lt b) compound1 lt- If altb is true
execute this. else compound2 lt- If altb is
false execute this.
22
One Statement ? No
If (fltg) statement1
if (dlte) statement1 else statement2
Null else branch!
If (hi) else statement2
Seldom used!
23
Book 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.

24
Nested If Statements
  • Obviously, a compound statement may contain more
    if statements.
  • Logic can be a bit tricky if they get too deep!

25
Switch Statement
switch( size ) case 2 case 3
printf(small\n) break case 4
printf(medium\n) break
  • A range of choices.

26
Hand 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?
27
Hand 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
28
Hand 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
29
Hand 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
30
Hand 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
31
Hand 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
32
Hand 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
33
Hand 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
34
Hand 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
35
Hand 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
36
Hand 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
37
Hand 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
Write a Comment
User Comments (0)
About PowerShow.com