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)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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)
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 <statements> 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;elsestatement3;}elsestatement4;//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(){
else cout<<3<<endl;//statement3;cout<<4<<endl;//statement4; /*this statement is not in the above else block, it will always be executed whenever we enter if(x>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 */