Title: COMP102
1COMP102 Programming Fundamentals I
- LA2B (Mon 5-7pm)
- LA2E (Fri 3-5pm)
- LA2F (Fri 5-7pm)
- TA Jackie Lo
2Lecture Review
- Logically controlled loops
- Pre-test loop
- while loops
- Post-test loop
- do-while loops
- Counter-controlled loops
- for loops
3Lecture Review
- while Loop
- Use Boolean expression to control the iteration
- Check condition before action
- Syntax
- while (condition fulfilled)
- action
- Operation
- Whenever (condition true)
- Repeat the action
4Lecture Review
- do-while Loop
- Use Boolean expression to control the iteration
- Check condition after action
- Syntax
- do action
- while (condition fulfilled)
- Operation
- Repeat the action
- Whenever (condition true)
5Lecture Review
- for Loop
- Use counter to control the iteration
- Update counter and check condition after action
- Syntax
- for ( initial condition update)
- action
6Lecture Review
- Exam skills (Reading code segment)
- Input 5
- Output?
- Result 120
cin gtgt n f 1 c 1 while (c lt n) f
c c 1 coutltltResult ltltfltltendl
7Lecture 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
8Summary
- By the end of this lab, you should be able to
- Implement loops
- for/while/do-while statement
- Distinguish between pre-/post-increment
9Lab3
include ltiostreamgt using namespace std int
main() int i 1 cout ltlt "i " ltlt i ltlt
endl // postfix form cout ltlt "i " ltlt
i ltltendl cout ltlt "i " ltlt i ltlt endl i
1 // prefix form cout ltlt "i " ltlt i
ltltendl cout ltlt "i " ltlt i ltlt endl return 0
10Lab3
//repeat //initialization //repeat //rep
eat //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
ltlt"Good bye!"ltltendl