COMP 102 Programming Fundamentals I - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

COMP 102 Programming Fundamentals I

Description:

Repeat the 'action' Until (condition = false) COMP 102 Lab 04. 4. The do-while Statement ... Repeat the 'action' else. Exit the loop. COMP 102 Lab 04. 5. The ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 12
Provided by: typey73
Category:

less

Transcript and Presenter's Notes

Title: COMP 102 Programming Fundamentals I


1
COMP 102Programming Fundamentals I
  • Presented by Timture Choi

2
Loops
  • Logically controlled loops
  • Pre-test loop
  • while loops
  • Post-test loop
  • do-while loops
  • Counter-controlled loops
  • for loops

3
The while Statement
  • Use a Boolean expression
  • To control the continuity of an iteration rather
    than a counter
  • Syntax
  • while (condition fulfilled)
  • action
  • Operation
  • Repeat the action
  • Until (condition false)

4
The do-while Statement
  • Syntax
  • do action
  • while (condition fulfilled)
  • At least do action once
  • Operation
  • Execute action
  • Evaluate condition
  • if (condition true)
  • Repeat the action
  • else
  • Exit the loop

5
The for Statement
  • Used the loop variable
  • To control the number of times the iteration
    executes
  • Syntax
  • for (initialization condition update)
  • action
  • E.g.

6
Nested Loops
  • Loops within loops
  • Nested if

for (initialization condition_1 update)
while (condition) action_1
action_2
7
Stop a Loop
  • break
  • Terminate a loop
  • Execute the next statement followed with the loop
    statement
  • continue
  • Terminate the current iteration
  • Transfer the control to the statement which
    contains the controlling expression of the loop

8
Forever Loop
  • Structure
  • while (true)
  • action
  • break
  • Should associated with break

9
for vs while
  • while loop
  • Provide a more flexible way of iteration than a
    for loop
  • for loop
  • Provide a more efficient way for loop execution

10
Increment/Decrement
  • / --
  • 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

11
SUMMARY
  • By the end of this lab, you should be able to
  • Implement loops
  • for/while statement
  • Stop a loop
  • Distinguish between pre-/post-increment
Write a Comment
User Comments (0)
About PowerShow.com