290121 Computer Programming - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

290121 Computer Programming

Description:

Condition Flow Chart. if (x 100) x = x 1; else y = y 1; x ... pulse = 80; 11/10/09. 12. Multiple actions. ???????????????????????????????????????????????? ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 40
Provided by: akaraso
Category:

less

Transcript and Presenter's Notes

Title: 290121 Computer Programming


1
290121 Computer Programming
  • 5. ???????????????????????????
  • CONDITIONS

?. ????????? ????????? ????????????????
???????????????? ????????
2
??????????
  • if, if-else
  • comparison
  • combining
  • DeMorgans Laws
  • switch

3
Conditional 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

4
Conditional ( 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

5
Condition Flow Chart
if (x lt 100) x x1 else y y1
yes
x lt 100 ?
x x1
no
y y1
6
Condition 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

7
Boolean Operators in Conditional Expressions
  • ???????????? Boolean
  • !
  • and or not

???????? temp gt 90.1 humidity gt 50.1 !
(salary lt 30000 work_year lt 4)
8
Value of conditional expressions
  • ?????????????????????????? TRUE ???? False
  • ?????? C ????????? boolean ??????????????????
    (integer)
  • FALSE ??????? 0
  • TRUE ??????????????????? 0
  • ?????????? 1
  • 1 ???????????????? relational operator
    ( lt, lt,gt,gt,,! ) ???????????????????? true

9
Formatting if statements
if (condition) statement else
statement
if (condition) statement
if (condition) statement1
statement2 else statement1
statement2
if (condition) statement1 statement2
10
if statements
  • if (condition)
  • statement

??? 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
11
Flow 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
12
Multiple actions
  • ????????????????????????????????????????????????
  • ??? compound statement
  • ????????????????????? ???????? block

statement1 statement2 ...
13
Multiple actions
  • ???????? ?????? compound statement

if (temperature gt 98.6) printf ( You have a
fever. \n) aspirin 2
14
if-else
  • if (balance gt withdrawal)
  • balance balance-withdrawal
  • printf (Withdraw Complete.\n)
  • else
  • printf (Insufficient Fund.\n)

???????????????????????
???????????????????????
15
if-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
/
16
Flow 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()
17
Nested 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)

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

19
Tax Example
Tax 0 18 22 28 31
  • Income
  • lt15,000
  • 15,000 - lt30,000
  • 30,000 - lt50,000
  • 50,000 - lt100,000
  • gt100,000

20
Simple 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
21
Cascaded 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.)
22
The 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)
23
Sort 2 Characters
  • Input two characters
  • Rearrange them in sorted order
  • Output them in sorted order

Example Input ra Output ar Input
nt Output nt
24
Pseudocode
  • 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
25
Program
  • / 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
26
Complex 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
27
if (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
28
Truth Tables for ,
  • P Q PQ PQ
  • T T T T
  • T F F T
  • F T F T
  • F F F F

29
Not (!)
  • 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
30
DeMorgans 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)

33
switch
  • switch (control expression)
  • case constant1
  • statement1
  • break
  • case constant2
  • statement2
  • break
  • .
  • .
  • .
  • default
  • statement

34
Longwinded 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/
35
Clearer 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/
36
Clearest 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/
37
switch 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/
39
switch 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
Write a Comment
User Comments (0)
About PowerShow.com