For Loops - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

For Loops

Description:

Challenge: Racer. Simulate a race that says 'Now on lap X' for 10 laps. ... Backwards Racer Algorithm. New Program backRacer by me. New integer variable lap ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 22
Provided by: wallyC
Category:
Tags: loops | racer

less

Transcript and Presenter's Notes

Title: For Loops


1
For Loops
2
Challenge Racer
  • Simulate a race that says Now on lap X for 10
    laps.
  • Make X vary, so it says 1, then 2, then 3
  • Use only one output statement!

3
Loops
  • A loop is used for repetitive behaviour
  • A set of commands is placed in a code block
  • A condition determines when the block stops
    repeating

4
The For Loop
  • Used when you know how many times something will
    happen
  • Great for counting
  • Also often used with lists

5
Parts of a For Loop
  • Sentry Variable
  • Initial Value
  • Looping Condition
  • Increment Statement

6
Sentry Variable
  • A special variable designed to control the loop
  • In for loops, usually an integer
  • Use a meaningful name when practical
  • Traditionally use i if no other name makes sense

7
Initial Value
  • Give sentry some meaningful initial value
  • Usually 0 or 1

8
Looping Condition
  • A condition
  • Always involves sentry variable
  • Indicates when loop should continue
  • If condition is true, loop will repeat
  • When condition becomes false, loop will end

9
Increment Statement
  • A line of code
  • Always involves sentry variable
  • Changes value of sentry
  • Usually i, i--, I something
  • Must make it possible for condition to become
    false eventually

10
Racer Algorithm
  • New Program Racer by me
  • New integer variable lap starts at 1
  • For loop with lap going from 1 to 10 by 1
  • Output Now on lap lap
  • End For loop
  • End racer

11
Challenge Backwards Racer
  • Simulate a race that says Now on lap X for 10
    laps.
  • This time go backwards!
  • Make X vary, so it says 10, then 9, then 8
  • Use only one output statement!

12
Counting Backwards
  • Use same for loop elements
  • Use larger value for initial sentry value
  • Decrement variable each time through
  • Check for smaller result

13
Backwards Racer Algorithm
  • New Program backRacer by me
  • New integer variable lap starts at 10
  • For loop with lap going from 10 to 1 by -1
  • Output Now on lap lap
  • End For loop
  • End racer

14
Counting by 5 Algorithm
  • New Program byFive by me
  • New integer variable lap starts at 0
  • For loop with lap going from 0 to 50 by 5
  • Output Now on lap lap
  • End For loop
  • End racer

15
Code Tracing
  • Make a chart
  • Make each variable a column head
  • Make a column for each condition
  • Make a column for output

16
Code Tracing Contd
  • Walk through code one line at a time
  • Each time a variable changes, change it on the
    chart
  • Each time you get to a condition statement,
    evaluate the condition

17
Code Tracing III
  • For conditions, write TRUE or FALSE
  • Write any output

18
Code Tracing Example
19
(No Transcript)
20
Ordinary Slide
  • Level 1
  • Level 2
  • Level 3

21
Code
  • My code
  • More Code
  • More Code
Write a Comment
User Comments (0)
About PowerShow.com