CPS120 Introduction to Computer Science - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

CPS120 Introduction to Computer Science

Description:

Title: CPS120 Introduction to Computer Science Author: User Last modified by: Paul J. Millis Created Date: 3/28/2002 1:31:01 AM Document presentation format – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 21
Provided by: wcc60
Learn more at: http://space.wccnet.edu
Category:

less

Transcript and Presenter's Notes

Title: CPS120 Introduction to Computer Science


1
CPS120 Introduction to Computer Science
  • Iteration (Looping)

2
Looping
  • Loops allow your program to repeat groups of
    statements a specified number of times. Loops are
    an example of an iteration structure.

3
Iterate
  • A program loop is a form of iteration. A computer
    can be instructed to repeat instructions under
    certain conditions.

4
Looping with Keyword goto
  • Uses concepts covered in decision making
    discussion
  • Implemented with a simple if..then structure
  • loop counter
  • cout ltlt"Processing Counter "ltlt counter ltlt
    endl
  • if (counter lt 5)
  • goto loop

5
Iteration Control Structures
  • Iteration control structures are looping
    mechanisms.
  • Loops repeat an activity until stopped. The
    location of the stopping mechanism determines how
    the loop will work
  • Leading decisions
  • Trailing decisions

6
Leading Decisions
  • If the stop is at the beginning of the iteration,
    then the control is called a leading decision.
  • The command DO WHILE performs the iteration and
    places the stop at the beginning.

7
For Loops
  • A for loop always executes a specific number of
    iterations.
  • Use a for loop when you know exactly how many
    times a set of statements must be repeated
  • A for loop is called a determinant or definite
    loop because the programmer knows exactly how
    many times it will iterate

8
Syntax of a for Loop
  • for (initializing expression control expression
    step expression) // one or more
    statements
  • The initializing expression sets the counter
    variable for the loop to its initial value.
  • The control expression ends the loop at the
    specified moment.
  • The step expression changes the counter variable
  • Semi-colons, not commas, divide the expressions

9
Things to Remember About For Loops
  • It is possible to have variable increase by a
    value other than one
  • for (num 1 num lt 10 num num 3)
  • It is possible to initialize the loop variable
    within the initializing expression. But I
    recommend against it
  • Declare loop variables at the top of the function
    where they belong
  • If an if statement only contains one body
    statement, the compiler doesn't require the use
    of curly braces

10
WHILE Loop
11
While Loops
  • A while loop does not necessarily iterate a
    specified number of times
  • As long as its control expression is true, a
    while loop will continue to iterate
  • An indeterminate or indefinite loop because only
    at run-time can it be determined how many times
    it will iterate
  • While is considered a top-checking loop
  • The control expression is located on the first
    line of code
  • If the control expression initially evaluates to
    FALSE, the loop will not execute even once.

12
While Loop Syntax
  • while (control expression) // one or
    more statements
  • The control expression must evaluate to TRUE in
    order for the while loop to iterate even once

13
While (true) Loops
  • This type of loop will never end unless you put a
    break in it
  • while (true)
  • counter
  • if (counter gt 10)
  • break

14
Trailing Decisions
  • If the stop is at the end of the iteration, the
    control mechanism is called a trailing decision.
  • The command DO / WHILE performs the iteration and
    puts the stop at the end of the loop.

15
DO WHILE Loop
Loop statement a
16
Do While Loops
  • As with a while loop, a do while loop does not
    necessarily iterate a specified number of times
  • A do while loop will iterate at least one time
    because its control expression is placed at the
    end of the loop
  • Considered to be an indeterminate or indefinite
    loop
  • Considered to be a bottom-checking loop, since
    the control expression is located after the body
    of the loop

17
Do While Syntax
  • do // body statements would be placed
    herewhile (control expression)
  • Don't forget to include the required semicolon
    after the control expression

18
Break and Continue Statements
  • A break statement is used to stop the execution
    of a loop immediately and to continue by
    executing the statement that comes directly after
    the loop
  • A continue statement is used to stop the
    execution of the statements in the loop's body on
    that particular iteration and to continue by
    starting the next iteration of the loop

19
Nested Loops
  • A loop of any kind may be placed in another loop
    (of any kind).
  • Be sure to entirely encapsulate the inner loop
    inside of the outer loop, otherwise an error is
    sure to occur.
  • Two loops are considered to be nested loops if
    one is enclosed within the other

20
In Summary
  • Loops
  • goto loops -- simple ifthen structure
  • for -- fixed number of loops
  • while -- may never be run
  • while (true) -- always true, needs break
  • do while -- always run once
  • continue causes while, do while, and for loops
    to start over
  • break causes while, do while, for and switch
    statements to end
Write a Comment
User Comments (0)
About PowerShow.com