Saturday, December 16, 2006 - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Saturday, December 16, 2006

Description:

if (withdrawal balance) cout 'Insufficient funds.' endl; ... int i, j, k ; for( i=0; i 10; i-- ) cout i; for (i=0; i 10; ) j=i 30; k=j 30; Fibonacci ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 37
Provided by: Erud
Category:

less

Transcript and Presenter's Notes

Title: Saturday, December 16, 2006


1
Saturday, December 16, 2006
  • The whole of the development and operation of
    analysis are now capable of being executed by
    machinery
  • As soon as an Analytical Engine exists, it will
    necessarily guide the future course of science.
  • - Charles Babbage (1792 - 1871)

2
  • Office hours today

3
Another way of writing this?
if(num_credits lt 0) cout ltlt "Come on,
get real" ltlt endl else if (num_credits lt
12) cout ltlt "Part-time student" ltlt
endl else if (num_credits lt 18)
cout ltlt "Full-time student" ltlt endl else
cout ltlt "Glutton for punishment" ltlt
endl
4
Nested if statements
  • The ltstatementsgt inside the braces can contain
    any valid C statements, including if
    statements!
  • // some other code here
  • char answer
  • if (withdrawal gt balance)
  • cout ltlt "Insufficient funds." ltlt endl
  • cout ltlt "Do you want to see your balance?
    "
  • cin gtgt answer
  • if (answer 'y')
  • coutltlt "Your balance is "ltltbalanceltlt
    endl
  • else
  • cout ltlt "Here is your money." ltlt endl
  • cout ltlt "Good bye." ltlt endl

5
Nested if statements
if (xgty) if (xgtz) statement1 if
(xgtp) statement2 else statement3 else statemen
t4 //bad style- no indentation (code with proper
indentation on next slide)
6
Nested if statements
if (xgty) if (xgtz) statement1 if
(xgtp) statement2 else
statement3 else statement4 /else statement
always refers to nearest if statement that is
within same block as else and not already
associated with another else/
7
Nested if statements // what is wrong here?
int main() int x10, y2, z12, p13 if
(xgty) if (xgtz) coutltlt1ltltendl//statement1
if (xgtp) coutltlt2ltltendl//statement2 els
e coutltlt3ltltendl//statement3 else
coutltlt4ltltendl//statement4 else
coutltlt5ltltendl//statement5 return 0
8
Nested if statements // what is wrong here?
int main() int x10, y2, z12, p13 if
(xgty) if (xgtz) coutltlt1ltltendl//statement1
if (xgtp) coutltlt2ltltendl//statement2 els
e coutltlt3ltltendl//statement3 else
//Error coutltlt4ltltendl//statement4 else
coutltlt5ltltendl//statement5 return 0
9
Nested if statements // what is output here?
int main() int x10, y2, z12, p13 if
(xgty) if (xgtz) coutltlt1ltltendl//statement1
if (xgtp) coutltlt2ltltendl//statement2 els
e coutltlt3ltltendl//statement3 els
e coutltlt4ltltendl//statement4 else
coutltlt5ltltendl//statement5 return 0
10
  • Output is 4

11
SELF TEST // what is wrong here?
int main() int x10, y2, z12, p13 if
(xgty) if (xgtz) coutltlt1ltltendl//statement1
if (xgtp) coutltlt2ltltendl//statement2 els
e coutltlt3ltltendl//statement3 coutltlt4ltlte
ndl//statement4 else coutltlt5ltltendl//statem
ent5 return 0
12
SELF TEST // what is wrong here?
int main() int x10, y2, z12, p13 if
(xgty) if (xgtz) coutltlt1ltltendl//statement1
if (xgtp) coutltlt2ltltendl//statement2 els
e coutltlt3ltltendl//statement3 coutltlt4ltlten
dl//statement4 /this statement is not in the
above else block, it will always be executed
whenever we enter if(xgty) block. Try this code
for x10, y2, z3, p13 and also for x10, y2,
z13, p3 / else coutltlt5ltltendl//statement5
return 0
13
  • char alarm
  • int fuel_quantity, temperature, pressure
  • coutltlt"Enter alarm fuel_quantity temperature
    pressure\n"
  • cingtgt alarm gtgt fuel_quantity gtgt temperaturegtgt
    pressure
  • if (alarm'y')
  • if(fuel_quantity lt10)
  • coutltlt"Add more fuel\n"
  • else
  • if (temperature gt90)
  • if (pressure gt100)
  • coutltlt"RED ALERT! Shut down motor\n"
  • else
  • coutltlt"Turn on pressure valve\n"
  • else
  • coutltlt"Check temperature again after 10
    minutes\n"

14
  • Loops of various sorts are used to repeat a set
    of statements some number of times.

15
  • Print numbers from 0 to 1000

16
  • int count_down3
  • while (count_down gt 0)
  • cout ltlt "Hello " count_down - 1

17
  • int count_down3
  • while (count_down gt 0)
  • cout ltlt "Hello " //count_down - 1
  • What happens now?

18
  • int x 10
  • while ( x gt 0)
  • cout ltlt x ltlt endl x x 3

19
  • int x 10
  • while (x gt 0)
  • cout ltlt x ltlt endl x x 3
  • Output using the comparison x lt 0 instead of x gt
    0?

20
What happens here?
  • int x 1
  • while (x ! 12)
  • cout ltlt x ltlt endl
  • x x 2

21
Print the odd numbers less than 12
  • int x 1
  • while (x ! 12)
  • cout ltlt x ltlt endl
  • x x 2
  • How to fix it?

22
While loops
  • What's wrong with this?
  • int x 10
  • while ( x gt 0 )
  • cout ltlt x ltlt endl
  • x--

23
  • While-loop and for-loop

24
  • int x 1
  • while (x lt 12)
  • coutltltxltltendl
  • x x 2

25
The for loop
for (initialization expression
increment) //statements here Example
flowchart
26
  • int x 1
  • while (x lt 12)
  • coutltltxltltendl
  • x x 2

27
  • int x 1
  • while (x lt 12)
  • coutltltxltltendl
  • x x 2
  • for (x1 xlt12 xx2)
  • coutltltxltltendl

28
int i for( i0 ilt10 i ) cout ltlt i ltlt "
"
29
int i for( i0 ilt10 i ) cout ltlt i ltlt "
" int i for (i0 ilt10 i) cout ltlti ltlt
" "
30
int x, n100 for (x0 xltn x) xx1 coutlt
lt"x after end of loop is "ltltxltltendl
31
int x, n100 for (x0 xltn x) xx1 coutlt
lt"x after end of loop is "ltltxltltendl x is the
smallest even number gt n
32
SELF TEST
  • int x, n100
  • for (x0 xltn x)
  • coutltltxltltendl
  • xx1
  • coutltlt"x after end of loop is "ltltxltltendl

33
SELF TEST
  • int x, n100
  • for (x0 xltn x)
  • xx1
  • coutltltxltltendl
  • coutltlt"x after end of loop is "ltltxltltendl

34
The For Loop
int i for( i23 igt30 i ) cout ltlt i ltlt
" " for (i13 igt10 --i) cout ltlti ltlt"
"
35
The For Loop
What is wrong here? int i, j, k for( i0 ilt10
i-- ) coutltlti for (i0 ilt10
) ji30 kj30
36
  • Fibonacci numbers
  • F11
  • F22
  • FnFn-1 Fn-2
  • 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
    2584 4181 6765 10946 17711 28657
Write a Comment
User Comments (0)
About PowerShow.com