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 ( )
§Office hours today
Another way of writing this? if(num_credits < 0) { cout << "Come on, get real" << endl; } else if (num_credits < 12) { cout << "Part-time student" << endl; } else if (num_credits < 18) { cout << "Full-time student" << endl; } else { cout << "Glutton for punishment" << endl; }
Nested if statements The inside the braces can contain any valid C++ statements, including if statements! // … some other code here char answer; if (withdrawal > balance) { cout << "Insufficient funds." << endl; cout << "Do you want to see your balance? "; cin >> answer; if (answer == 'y') cout<< "Your balance is "<<balance<< endl; } else { cout << "Here is your money." << endl; } cout << "Good bye." << endl;
Nested if statements if (x>y){ if (x>z) statement1; if (x>p) statement2; else statement3; } else statement4; //bad style- no indentation (code with proper indentation on next slide)
Nested if statements if (x>y){ if (x>z) statement1; if (x>p) 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*/
Nested if statements // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; else cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}
Nested if statements // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; else //Error cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}
Nested if statements // what is output here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z){ cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; } else cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0; }
§Output is 4
SELF TEST // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}
SELF TEST // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; cout y) block. Try this code for x=10, y=2, z=3, p=13 and also for x=10, y=2, z=13, p=3 */ } else cout<<5<<endl;//statement5; return 0;}
char alarm; int fuel_quantity, temperature, pressure; cout<<"Enter alarm fuel_quantity temperature pressure\n"; cin>> alarm >> fuel_quantity >> temperature>> pressure; if (alarm=='y') { if(fuel_quantity <10){ cout<<"Add more fuel\n"; } else{ if (temperature >90){ if (pressure >100) cout<<"RED ALERT! Shut down motor\n"; else cout<<"Turn on pressure valve\n"; } else { cout<<"Check temperature again after 10 minutes\n"; } else { cout<<"No problem with motor\n"; }
§Loops of various sorts are used to repeat a set of statements some number of times.
§Print numbers from 0 to 1000
int count_down=3; while (count_down > 0) { cout << "Hello "; count_down -= 1; }
int count_down=3; while (count_down > 0) { cout << "Hello "; //count_down -= 1; } What happens now?
int x = 10; while ( x > 0) { cout << x << endl; x = x – 3; }
int x = 10; while (x > 0) { cout << x << endl; x = x – 3; } Output using the comparison x 0?
What happens here? int x = 1; while (x != 12) { cout << x << endl; x = x + 2; }
Print the odd numbers less than 12 int x = 1; while (x != 12) { cout << x << endl; x = x + 2; } How to fix it?
While loops What's wrong with this? int x = 10; while ( x > 0 ); { cout << x << endl; x--; }
While-loop and for-loop
int x = 1; while (x < 12) { cout<<x<<endl; x = x + 2; }
The for loop for (initialization; expression; increment) { //statements here } Example: flowchart
int x = 1; while (x < 12) { cout<<x<<endl; x = x + 2; }
int x = 1; while (x < 12) { cout<<x<<endl; x = x + 2; } for (x=1; x<12; x=x+2) { cout<<x<<endl; }
int i ; for( i=0; i<10; ++i ) { cout << i << " "; }
int i; for( i=0; i<10; ++i ) { cout << i << " " ; } int i; for (i=0; i<10; i++) { cout <<i << " " ; }
int x, n=100; for (x=0; x<n; x++){ x=x+1; } cout<<"x after end of loop is "<<x<<endl;
int x, n=100; for (x=0; x<n; x++){ x=x+1; } cout<<"x after end of loop is "<<x<<endl; x is the smallest even number >= n
SELF TEST int x, n=100; for (x=0; x<n; x++){ cout<<x<<endl; x=x+1; } cout<<"x after end of loop is "<<x<<endl;
SELF TEST int x, n=100; for (x=0; x<n; x++){ x=x+1; cout<<x<<endl; } cout<<"x after end of loop is "<<x<<endl;
The For Loop int i ; for( i=23; i>=30; ++i ) { cout << i << " "; } for (i=13; i>=10; --i) { cout <<i <<" "; }
The For Loop What is wrong here? int i, j, k ; for( i=0; i<10; i-- ) { cout<<i; } for (i=0; i<10; ) { j=i+30; k=j+30; }
Fibonacci numbers F1=1 F2=2 Fn=Fn-1 + Fn …