Title: 290121 Computer Programming
1290121 Computer Programming
- 5. ???????????????????????????
- CONDITIONS
?. ????????? ????????? ????????????????
???????????????? ????????
2??????????
- if, if-else
- comparison
- combining
- DeMorgans Laws
- switch
3Conditional Execution
- The power of computers lies to a large extent
with conditionals. - A conditional statement allows the computer to
choose a different execution path depending on
the value of a variable or expression, e.g. - Example
- if the withdrawal is more than the bank balance,
print an error - if today is my birthday, add one to my age
- if my grade is greater than 3.5, go to party
- if x is bigger than y then store x in z otherwise
story y in z
4Conditional ( if ) Statement
????????????????????????? condition ????????
if (condition) statement
- if (x lt 100) xx1
- if (withdraw gt balance) print_error()
- if (temperature gt 98.6) printf (You have a
fever.\n) - if (month birthmonth day birthday)
my_agemy_age1
5Condition Flow Chart
if (x lt 100) x x1 else y y1
yes
x lt 100 ?
x x1
no
y y1
6Condition Expressions
- ???????? logical ???? Boolean expressions
- ????????????????, ????????, arithmetic
expressions ??? relational operators ???? lt ,
lt, gt, gt, , ! - ?????????????????????????
- air_temperature gt 0.0
- 98.6 lt body_temperature
- marital_status M
- divisor ! 0
7Boolean Operators in Conditional Expressions
- ???????????? Boolean
- !
- and or not
???????? temp gt 90.1 humidity gt 50.1 !
(salary lt 30000 work_year lt 4)
8Value of conditional expressions
- ?????????????????????????? TRUE ???? False
- ?????? C ????????? boolean ??????????????????
(integer) - FALSE ??????? 0
- TRUE ??????????????????? 0
- ?????????? 1
- 1 ???????????????? relational operator
( lt, lt,gt,gt,,! ) ???????????????????? true
9Formatting if statements
if (condition) statement else
statement
if (condition) statement
if (condition) statement1
statement2 else statement1
statement2
if (condition) statement1 statement2
10if statements
??? condition ???????? (TRUE) ????????????????????
???????? if (????? if-statement)
?????????????????????????????? if
In pseudocode if denominator is not equal to
zero result is numerator /
denominator
In C if (denom ! 0)
result num / denom
11Flow of Control
temperature 98.1 if (temperature lt 98.6)
printf (You have a fever) pulse 80
temperature 99.7 if (temperature lt 98.6)
printf (You have a fever) pulse 80
12Multiple actions
- ????????????????????????????????????????????????
- ??? compound statement
- ????????????????????? ???????? block
statement1 statement2 ...
13Multiple actions
- ???????? ?????? compound statement
if (temperature gt 98.6) printf ( You have a
fever. \n) aspirin 2
14if-else
- if (balance gt withdrawal)
-
- balance balance-withdrawal
- printf (Withdraw Complete.\n)
-
- else
-
- printf (Insufficient Fund.\n)
-
???????????????????????
???????????????????????
15if-else Control Flow
yes
no
balance gt withdraw ?
balancebalance withdrawal printf (Withdraw
Complete.\n)
printf (Insufficient Funds.\n)
/ arrive here whether condition is TRUE of FALSE
/
16Flow of Control
bal 25 withdrawal 40 if (bal gt
withdrawal) bal bal withdrawal printf
(Withdraw Complete.\n) else printf
(Insufficient Fund.\n) eject_card()
bal 25 withdrawal 20 if (bal gt
withdrawal) bal bal withdrawal printf
(Withdraw Complete.\n) else printf
(Insufficient Fund.\n) eject_card()
17Nested if
- define BILL_SIZE 20
- if (balance gt withdrawal)
-
- balance balance withdraw
-
- else
-
- if (balance gt BILL_SIZE)
- printf (Try a smaller amount. \n)
- else
- printf (Go away! \n)
-
18Nested if
- if (x 5)
- if (y 5)
- printf (Both are 5. \n)
- else
- printf (x is 5, but y is not.\n)
- else
- if (y 5)
- printf (y is 5, but x is not.\n)
- else
- printf (Neither is 5. \n)
19Tax Example
Tax 0 18 22 28 31
- Income
- lt15,000
- 15,000 - lt30,000
- 30,000 - lt50,000
- 50,000 - lt100,000
- gt100,000
20Simple Solution
- if (income lt 15000)
- printf(No tax.)
- if (income gt 15000 income lt 30000)
- printf(18 tax.)
- if (income gt 30000 income lt 50000)
- printf(22 tax.)
- if (income gt 50000 income lt 100000)
- printf(28 tax.)
- if (income gt 100000)
- printf(31 tax.)
Only one will be true
21Cascaded if
- if (income lt 15000)
- printf(No tax.)
- else
- if (income lt 30000)
- printf(18 tax.)
- else
- if (income lt 50000)
- printf(22 tax.)
- else
- if (income lt 100000)
- printf(28 tax.)
- else
- printf(31 tax.)
if (income lt 15000) printf(No tax.) else if
(income lt 30000) printf(18 tax.) else
if (income lt 50000) printf(22 tax.)
else if (income lt 100000) printf(28
tax.) else printf(31
tax.)
22The First Character
- / read 3 character print the smallest /
- printf (Enter 3 chars )
- scanf (c c c, c1, c2, c3)
- first c1
- if (c2ltfirst)
- first c2
- if (c3 lt first)
- first c3
- printf (Alphabetically,
- the first is c, first)
c1 c2 c3 first h a t
? h a t h
(true) h a t a
(false) (printf a)
23Sort 2 Characters
- Input two characters
- Rearrange them in sorted order
- Output them in sorted order
Example Input ra Output ar Input
nt Output nt
24Pseudocode
- Input c1,c2
- if c2 comes before c1 in alphabet
- swap c1 and c2
- Output c1,c2
swap
c1
c2
Input c1,c2 if c2 comes before c1 in
alphabet Save c1 in temporary Assign c2 to
c1 Assign temporary to c2 Output c1,c2
2
c1
c2
1
3
temp
25Program
- / sort two characters and print in sorted order
/ - char c1,c2,temp
- printf(Enter 2 chars )
- scanf(c c,c1,c2)
- if (c2 ltc1) / swap if out of order /
-
- temp c1
- c1c2
- c2temp
-
- printf (In alphabetical order, they are
cc,c1,c2)
c1 c2 temp d a ?
TRUE d a d a a d a d d printf
ad
26Complex Conditionals
- AND (), OR (), NOT (!)
- Example
if (age lt 25) if (sex M)
insurance_rate insurance_rate 2
if (age lt 25) (sex M)
insurance_rate insurance_rate 2
27if (age lt 25) (sex M)
insurance_rate insurance_rate 2
int high_risk ... high_risk ( age lt 25 sex
M) if (high_risk) insurance_rate
insurance_rate 2
28Truth Tables for ,
- P Q PQ PQ
- T T T T
- T F F T
- F T F T
- F F F F
29Not (!)
- int high_risk
- ...
- high_risk (age lt25 sex M)
- if (high_risk)
- printf(Expensive rates.\n)
- if (!high_risk)
- printf(Cheap rates.\n)
P !P T F F T
30DeMorgans Laws
- if ( ! (age lt 25 sex M))
- printf (Cheap rates.\n)
is equivalent to
if (age gt 25 sex ! M)) printf (Cheap
rates.\n)
More generally ! (P Q) is equivalent to (!P
!Q) !(P Q) is equivalent to (!P !Q)
31????????????????????
- if (xgt10) /no action or null
statement/ - printf(xgt10) /Always done (why?)/
Recall that any non-zero integer value is
true if (x) printf(x is nonzero)
/works,but bad style/
if (x 10) /should be , but its not a
syntax error/ printf(xgt10)
32- No if (0 lt x lt 10)
- printf( x is between 0 and 10. \n)
- Yes if (0lt x x lt10)
- printf(x is between 0 and 10. \n)
33switch
- switch (control expression)
-
- case constant1
- statement1
- break
- case constant2
- statement2
- break
- .
- .
- .
- default
- statement
34Longwinded if
- / How many days in a month? /
- if (month 1) days 31
- else if (month 2) days 28
- else if (month 3) days 31
- else if (month 4) days 30
/Jan/
/Feb/
/Mar/
/Apr/
/need 12 of these/
35Clearer Style
- if (month 9 month 4 month 6
month 11) - days 30
- else if (month 2)
- days 28
- else
- days 31
/Sep,Apr/
/Jun,Nov/
/All of the rest/
36Clearest switch
- / How many days in a month? /
- switch (month)
- case 2
- days 28 break
- case 9
- case 4
- case 6
- case 11
- days 30 break
- default
- days 31
-
- printf( There are d days in that month.\n,
days)
/Feb/
/Sep/
/April/
/June/
/Nov/
/All of the rest/
37switch Flow of control
month 6 switch (month) case 2 days 28
break case 9 case 4 case 6 case
11 days 30 break default days
31 printf( There are d days in that
month.\n, days)
/Feb/
/Sep/
/April/
/June/
/Nov/
/All of the rest/
38????????????????????? switch
month 6 switch (month) case 2 days 28
case 9 case 4 case 6 case 11 days
30 default days 31 printf( There are
d days in that month.\n, days)
break missing where?
/Feb/
/Sep/
/April/
/June/
/Nov/
/All of the rest/
39switch on char
- char marital_status
- printf(Enter marital status (M,S) )
- scanf(c, marital_status)
- switch (marital_status)
- case m
- case M
- printf(Married\n) break
- case s
- case S
- printf(Single\n) break
- default
- printf(Sorry, I dont recognize that
code.\n)
int or char expression