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 Logically controlled loops  Pre-test loop while loops  Post-test loop do-while loops Counter-controlled loops  for loops

3 Lecture Review while Loop  Use Boolean expression t o control the iteration  Check condition before action Syntax  while (condition fulfilled)  { action; } Operation  Whenever (condition = true) Repeat the “ action ”

4 Lecture Review do-while Loop  Use Boolean expression t o control the iteration  Check condition after action Syntax  do { action; }  while (condition fulfilled) Operation  Repeat the “ action ”  Whenever (condition = true)

5 Lecture Review for Loop  Use counter t o control the iteration  Update counter and check condition after action Syntax  for ( initial; condition; update) {action;}

6 Lecture Review Exam skills (Reading code segment) Input: 5 Output?  Result = 120 cin >> n; f = 1; c = 1; while (c <= n){ f *= c; c += 1; } cout<<“Result = “<<f<<endl;

7 Lecture Review ++ / --  Special operators for incrementing / decrementing an integer by 1 E.g.  j = k++; j = k; k = k + 1;  j = --k; k = k – 1; j = k;

8 Summary By the end of this lab, you should be able to:  Implement loops for/while/do-while statement  Distinguish between pre-/post-increment

9 Lab3 Prefix & Postfix shortcut #include using namespace std; int main() { int i = 1; cout << "i = " << i << endl; // postfix form cout << "i++ = " << i++ <<endl; cout << "i = " << i << endl; i = 1; // prefix form cout << "++i = " << ++i <<endl; cout << "i = " << i << endl; return 0; }

10 Lab3 Use Loop … //repeat //initialization … //repeat //Ask user to input a guess … // whenever guess not valid //Now: guess is valid // According to the guess, // output Too high or Too low … //increase no of attempts … // whenever guess != random no //Now: guess = random no //According to no of attempts, output comments … //repeat //Ask user to continue … //whenever respond not valid //whenever user wants to continue cout <<"Good bye!"<<endl;


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