Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham...
-
date post
19-Dec-2015 -
Category
Documents
-
view
214 -
download
1
Transcript of Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham...
Friday, December 22, 2006
When you have a hammer, everything starts looking like
a nail.
- Abraham Maslow
break review
for (i=0; i<10; i++){
…
for (j=0; j<100; j++){
…
if(some_cond) break;
}
}
What is wrong here?
for (i=0; i<10; i++){
…
}
if (some_cond) {
…
break;
}
The Switch Statementint main(){ int choice; cout << "Help on:\n\n"; cout << "1. for\n"; cout << "2. if\n"; cout << "3. switch\n\n"; cout << "Enter choice: (1-3): "; cin >> choice;
The Switch Statementswitch(choice) { case 1: cout << "for is C++'s most versatile loop.\n"; break; case 2: cout << "if is C++'s conditional branch statement.\n"; break; case 3: cout << "switch is C++'s multi-way branch statement.\n"; break; default: cout << "You must enter a number between 1 and 3.\n"; } return 0;}
The Switch Statement switch (expression) {
case constant1:statements;break;
case constant2:statements;break;
case constant3:statements;break;
default:statements;
}
The Switch StatementThe switch statement must evaluate to either a character or an integer value (Floating point expressions are not allowed)
No case constants in the same switch can have identical values
Switch can test only for equality
switch(choice) { case 1: cout << "for is C++'s most versatile loop.\n"; break; case 2: cout << "if is C++'s conditional branch statement.\n"; break; case 3: cout << "switch is C++'s multi-way branch statement.\n"; break; default: cout << "You must enter a number between 1 and 3.\n"; } return 0;}
Self Test: Alternate if-else statements?
The Switch Statementint main(){ int i; for(i=0; i<5; i++) { switch(i) { case 0: cout << "less than 1\n"; case 1: cout << "less than 2\n"; case 2: cout << "less than 3\n"; case 3: cout << "less than 4\n"; case 4: cout << "less than 5\n"; } cout << “\n”; } return 0;}
less than 1less than 2less than 3less than 4less than 5
less than 2less than 3less than 4less than 5
less than 3less than 4less than 5
less than 4less than 5
less than 5
Nested if-else BAD CODING STYLEif (code == 10)
cout<<“Too hot - turn off equipment\n”;else{
if (code == 11)cout<<“Caution !\n”;
else{ if (code==13)
cout<<“Turn on fan\n”;else
cout<<“Normal Mode\n”;}
}
The Switch Statementswitch (code){
case 10:cout<<“Too hot - turn off equipment\n”;break;
case 11:cout<<“Caution! recheck in 5 mins\n”;break;
case 13:cout<<“Turn on fan\n”;break;
default:cout<<“Normal Mode\n”;break:
}
The Switch Statementswitch(i) { case 1: case 2: case 3: //do_something_here; break; case 4: //do_something_else_here; break;}
The Switch Statementswitch(op_code) { case ‘N’ : case ‘R’: cout<<“Normal operating range\n”;
break; case ‘M’: cout<<“Maintenance needed\n”;
break; default: cout<<“Error in code value\n”;
break;}
Nested Switchchar ch1, ch2;cin>>ch1>>ch2;switch(ch1) {case 'A':
cout << "This A is part of outer switch\n";switch(ch2) {case 'A':
cout << "This A is part of inner switch\n";break;
case 'B': cout << "This B is part of inner switch\n";
}break;
case 'B': cout << "This B is part of outer switch\n";break;
}
Equivalent statements?
Nested Switchchar ch1, ch2;cin>>ch1>>ch2;switch(ch1) {case 'A':
cout << "This A is part of outer switch\n";switch(ch2) {case 'A':
cout << "This A is part of inner switch\n";break;
case 'B': cout << "This B is part of inner switch\n";
}break;
case 'B': cout << "This B is part of outer switch\n";break;
}
Equivalent statements?
Functions
T1
T2
A/DConverter
A/DConverter
AmplifierDisplaycircuit
Motor Control
AmplifierTemperature
DifferenceAlarm
Td1 Din Dout
Mout
AoutTd2
Tdin
Hardware design Subparts or modules Interface
Software design
int main(){double T1, T2, Td1, Td2, Din, Tdin, Dout, Mout, Aout;cin>>T1>>T2;Td1 = AtoD_Converter(T1);Td2 = AtoD_Converter(T2);Din = Amplifier(Td1);Tdin = Amplifier(Td2);Dout = DisplayCircuit(Din);Mout = MotorControl(Din);Aout = TemperatureAlarm(Din, Tdin);//…
T1
T2
A/DConverter
A/DConverter
AmplifierDisplaycircuit
Motor Control
AmplifierTemperature
DifferenceAlarm
Td1 Din Dout
Mout
AoutTd2
Tdin
Easy to understandEasier to changeEasier to test and debug.
Helps in dealing with large programs.
Function PrototypeA function prototype tells you all you need
to know to write a call to the functionPrototypes are normally placed before the
main part of your program return_type Function_Name ( Parameter_List );
Function PrototypeA function prototype tells you all you need
to know to write a call to the functionPrototypes are normally placed before the
main part of your program return_type Function_Name ( Parameter_List );Parameter_list is a comma separated list of parameters:Type1 Parameter_1, Type2 Parameter_2 ,
Type3 Parameter_3 …. TypeN Parameter_N
int sum(int x, int y);int main(){ int answer;cout<<“In main”;answer = sum(2,3);cout<<answer;
return 0;}int sum (int x, int y){int z=x+y;cout<<“In sum”;return z;
}
A function prototype declares the function prior to its definition.
Declaration is done before the first time the function is called.
Three places where variables are declared:
1. Inside functions: local variables
2. In the definition of function parameters: formal parameters
3. Outside of all functions: global variables
local variables are created when function is called and destroyed when function is exited.
formal parameters are used like local variables. Their value is lost once the function terminates. They have a special task of receiving the value of arguments.
global variables hold their value throughout the lifetime of your program
Some functions may not have a return value.
void can be used to indicate that the function does not return a value.
Some functions may not have formal parameters.
void can be used to indicate that the function does not take any formal parameters.
A few examples …
void function1 (int a , int b);int function2 (void)void function3 (void)int function4 (int a, int b);
Examples of Functions#include <iostream.h>
void function1(void);//function prototype
int main(void){
cout<<"I am in main"<<endl;
function1();
cout<<"back from function1 to main"<<endl;
return 0;
}
void function1(void){
cout<<"I am in function1"<<endl;
}
Examples of Functions
I am in main
I am in function1
back from function1 to main