COMP102 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

COMP102

Description:

Special operators for incrementing / decrementing an integer by 1. E.g. j = k ; j = k; ... between pre-/post-increment. Lab3. Prefix & Postfix shortcut ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 11
Provided by: jack52
Category:

less

Transcript and Presenter's Notes

Title: COMP102


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 to 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 to 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 to 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 gtgt n f 1 c 1 while (c lt n) f
c c 1 coutltltResult ltltfltltendl
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 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
10
Lab3
  • Use Loop

//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
Write a Comment
User Comments (0)
About PowerShow.com