Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.

Similar presentations


Presentation on theme: "COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo."— Presentation transcript:

1 COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo

2 Lecture Review Scope of variables  Every variable has living area, called scope  The variable will be killed once out of scope  Scope Starting:After the variable is declared Ending:At the end of the declared area

3 Lecture Review int y = 38; void fun( int, int); int main( ){ int z=47; while (z<400){ int a = 90; z += a++; z++; } y = 2 * z; fun(1, 2); return 0; } void fun(int s, int t){ int r = 12; s = r + t; int i = 27; s += i; } scope of i scope of r scope of s & t scope of a scope of z scope of y scope of fun

4 Exercise - 3 int A,B,C,D; void Two( int A, int B, int& D) { B = 21; D = 23; cout << A << " " << B << " " << C<< " " << D << endl; } void One( int A, int B, int& C) { int D; // Local variable A = 10; B = 11; C = 12; D = 13; cout << A << " " << B<< " " << C<< " " << D << endl; Two(A,B,C); } int main() { A = 1; B = 2; C = 3; D = 4; One(A,B,C); cout << A << " " << B << " " << C<< " " << D << endl; Two(A,B,C); cout << A << " " <<B << " " << C << " " << D << endl; return 0; }

5 Answer Output ABCD in One = 10 11 12 13 ABCD in Two = 10 21 23 23 ABCD in Main = 1 2 23 4 ABCD in Two = 1 21 23 23 ABCD in Main = 1 2 23 4

6 Lecture Review Multiple Selection Statement value1 action 1 value2 action 2 value3 action 3 value4 action 4 multiway expression

7 Lecture Review switch Statement  Syntax:  The selector expression can only be  bool, integer, char, or enum constant switch ( ) { case : ; break; case : ; break; case : ; break; default : ; }

8 Lecture Review Note:  If break encountered Go to end of the switch statement  Otherwise continue the next execution int score=95; switch(int(score)/10){ case 10: case 9: cout << "Grade = A" << endl; case 8: cout << "Grade = B" << endl; case 7: cout << "Grade = C" << endl; case 6: cout << "Grade = D" << endl; default:cout << "Grade = F" << endl; } Output Grade = A Grade = B Grade = C Grade = D Grade = F

9 Lecture Review Common questions in exam  Change if-else-if statement to switch if (choice == 'b' || choice == 'B') balance+=bet(); else if (choice == 's' || choice == 'S') slot(balance, Jackpot); else if (choice == 'c' || choice == 'C') cash(balance); elsecout <<"Wrong choice"<<endl; switch (choice) { case 'b': case 'B': balance+=bet(); break; case 's': case 'S': slot(balance, Jackpot); break; case 'c': case 'C': cash(balance); break; default: cout <<"Wrong choice"<<endl; break; }

10 Summary By the end of this lab, you should be able to:  Identify the scope of different identifiers  Declared global/local variables/constants  Use switch statement

11 Announcement Mid-term  Next Friday (3 rd Nov) 6:30-8:30pm Review Session  Room 1403  Next Tue (31 st Oct) 6-7pm English  Next Wed (1 st Nov) 6-7pm Cantonese Q&A Session  Next Fri (3 rd Nov) 3-6pm

12 Lab7 Use functions & Scope of variable Download the template from website MUST NOT add any global variables If you cannot finish this lab on time, please schedule a time with me.  Tutorial & pre-test


Download ppt "COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo."

Similar presentations


Ads by Google