34
Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

  • View
    215

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

Friday, December 22, 2006

When you have a hammer, everything starts looking like

a nail.

- Abraham Maslow

Page 2: 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;

}

}

Page 3: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

What is wrong here?

for (i=0; i<10; i++){

}

if (some_cond) {

break;

}

Page 4: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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;

Page 5: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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;}

Page 6: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

The Switch Statement switch (expression) {

case constant1:statements;break;

case constant2:statements;break;

case constant3:statements;break;

default:statements;

}

Page 7: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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

Page 8: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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?

Page 9: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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;}

Page 10: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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

Page 11: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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”;}

}

Page 12: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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:

}

Page 13: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

The Switch Statementswitch(i) { case 1: case 2: case 3: //do_something_here; break; case 4: //do_something_else_here; break;}

Page 14: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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;}

Page 15: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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?

Page 16: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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?

Page 17: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

Functions

Page 18: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

T1

T2

A/DConverter

A/DConverter

AmplifierDisplaycircuit

Motor Control

AmplifierTemperature

DifferenceAlarm

Td1 Din Dout

Mout

AoutTd2

Tdin

Page 19: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

Hardware design Subparts or modules Interface

Software design

Page 20: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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

Page 21: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

Easy to understandEasier to changeEasier to test and debug.

Page 22: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

Helps in dealing with large programs.

Page 23: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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 );

Page 24: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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

Page 25: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow
Page 26: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow
Page 27: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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;

}

Page 28: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

A function prototype declares the function prior to its definition.

Declaration is done before the first time the function is called.

Page 29: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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

Page 30: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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

Page 31: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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.

Page 32: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

A few examples …

void function1 (int a , int b);int function2 (void)void function3 (void)int function4 (int a, int b);

Page 33: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

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;

}

Page 34: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow

Examples of Functions

I am in main

I am in function1

back from function1 to main