Title: Saturday, December 16, 2006
1Saturday, 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 3Another 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
4Nested 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
5Nested 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)
6Nested 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/
7Nested 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
8Nested 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
9Nested 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 11SELF 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
12SELF 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?
20What happens here?
- int x 1
- while (x ! 12)
- cout ltlt x ltlt endl
- x x 2
-
21Print the odd numbers less than 12
- int x 1
- while (x ! 12)
- cout ltlt x ltlt endl
- x x 2
-
- How to fix it?
22While loops
- What's wrong with this?
- int x 10
- while ( x gt 0 )
-
- cout ltlt x ltlt endl
- x--
-
23 24- int x 1
- while (x lt 12)
-
- coutltltxltltendl
- x x 2
-
25The 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
28int i for( i0 ilt10 i ) cout ltlt i ltlt "
"
29int i for( i0 ilt10 i ) cout ltlt i ltlt "
" int i for (i0 ilt10 i) cout ltlti ltlt
" "
30int x, n100 for (x0 xltn x) xx1 coutlt
lt"x after end of loop is "ltltxltltendl
31int x, n100 for (x0 xltn x) xx1 coutlt
lt"x after end of loop is "ltltxltltendl x is the
smallest even number gt n
32SELF TEST
- int x, n100
- for (x0 xltn x)
- coutltltxltltendl
- xx1
-
- coutltlt"x after end of loop is "ltltxltltendl
33SELF TEST
- int x, n100
- for (x0 xltn x)
- xx1
- coutltltxltltendl
-
- coutltlt"x after end of loop is "ltltxltltendl
34The For Loop
int i for( i23 igt30 i ) cout ltlt i ltlt
" " for (i13 igt10 --i) cout ltlti ltlt"
"
35The 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