Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow."— Presentation transcript:

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

2 break review for (i=0; i<10; i++){ … for (j=0; j<100; j++){ … if(some_cond) break; }

3 What is wrong here? for (i=0; i<10; i++){ … } if (some_cond) { … break; }

4 The Switch Statement int 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;

5 The Switch Statement 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; }

6 The Switch Statement switch (expression) { case constant1: statements; break; case constant2: statements; break; case constant3: statements; break; default: statements; }

7 The Switch Statement The 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

8 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?

9 The Switch Statement int 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; }

10 less than 1 less than 2 less than 3 less than 4 less than 5 less than 2 less than 3 less than 4 less than 5 less than 3 less than 4 less than 5 less than 4 less than 5

11 Nested if-else BAD CODING STYLE if (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”; }

12 The Switch Statement switch (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: }

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

14 The Switch Statement switch(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; }

15 Nested Switch char 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?

16 Nested Switch char 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?

17 Functions

18 T1 T2 A/D Converter A/D Converter Amplifier Display circuit Motor Control Amplifier Temperature Difference Alarm Td1Din Dout Mout Aout Td2 Tdin

19 §Hardware design l Subparts or modules l Interface §Software design

20 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/D Converter A/D Converter Amplifier Display circuit Motor Control Amplifier Temperature Difference Alarm Td1Din Dout Mout Aout Td2 Tdin

21 §Easy to understand §Easier to change §Easier to test and debug.

22 §Helps in dealing with large programs.

23 Function Prototype §A function prototype tells you all you need to know to write a call to the function §Prototypes are normally placed before the main part of your program return_type Function_Name ( Parameter_List );

24 Function Prototype §A function prototype tells you all you need to know to write a call to the function §Prototypes 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

25

26

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

28 §A function prototype declares the function prior to its definition. §Declaration is done before the first time the function is called.

29 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

30 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

31 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.

32 A few examples … §void function1 (int a, int b); §int function2 (void) §void function3 (void) §int function4 (int a, int b);

33 Examples of Functions #include 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; }

34 Examples of Functions I am in main I am in function1 back from function1 to main


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

Similar presentations


Ads by Google