Loops - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Loops

Description:

Loops Tonga Institute of Higher Education Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions For Loops Do Loops Loops ... – PowerPoint PPT presentation

Number of Views:167
Avg rating:3.0/5.0
Slides: 15
Provided by: DaveS203
Category:

less

Transcript and Presenter's Notes

Title: Loops


1
Loops
  • Tonga Institute of Higher Education

2
Introduction
  • Programs need to be able to execute tasks
    repeatedly.
  • Use loops to repeat actions
  • For Loops
  • Do Loops

3
Loops
  • Parts of a loop
  • Action
  • End Condition
  • Example Scanning items at checkout counter in
    grocery store.
  • Action Get a price for the item and add it to
    the bill.
  • End Condition All items have a price and have
    been added to the bill.

4
For Loop Introduction
  • Executes a definite number of times
  • All blue words are key words. They must be used
    exactly as shown to be understood by the
    computer.
  • All words in square brackets are optional
  • All words in angle brackets lt gt must be
    customized
  • Condition is evaluated before the body of the
    loop is executed.
  • Incrementation of counter takes place after the
    Body of Loop is executed.

For ltcountergt As ltdatatypegt ltstartgt To ltendgt
Step ltstepgt 'Body of the loop Next
ltcountergt
5
For Loop Basics
For ltcountergt As ltdatatypegt ltstartgt To ltendgt
Step ltstepgt 'Body of the loop Next
ltcountergt
Counter Name
Datatype
Start value
Can declare variable outside of For Loop
End value
Body of the loop
Can declare variable inside of For Loop
6
For Loop Increments and Optional Counter
For ltcountergt As ltdatatypegt ltstartgt To ltendgt
Step ltstepgt 'Body of the loop Next
ltcountergt
Assumes you increment 1 at the end of each loop
Same loop on previous page
Set increment value
Can put counter at end as reminder
7
For Loop Negative Increments
For ltcountergt As ltdatatypegt ltstartgt To ltendgt
Step ltstepgt 'Body of the loop Next
ltcountergt
Set increment value
Same loop on previous page
Numbers are reversed.
Set negative increment value
8
For Loop Exiting Loops
For ltcountergt As ltdatatypegt ltstartgt To ltendgt
Step ltstepgt 'Body of the loop Next
ltcountergt
You can quit a loop by using Exit For in the body
of the loop
9
Demonstration
  • For Loops

10
Do Loop Introduction
  • Executes an indefinite number of times
  • The body of the loop is executed as
  • Do While Condition is true
  • Do Until Condition is false
  • Developer controls when condition changes
  • Done in the body of the loop
  • If not done, the loop will never end
  • There are many ways to use a Do Loop
  • All blue words are key words. They must be used
    exactly as shown to be understood by the
    computer.
  • One of the words in curly brackets must be
    used
  • All words in angle brackets lt gt must be
    customized

Do While or Until ltconditiongt 'Body of the
loop Loop
Do 'Body of the loop Loop While or Until
ltconditiongt
11
Do Loop While vs. Until
Do While or Until ltconditiongt 'Body of the
loop Loop
Keep looping while the condition is true
Condition
Stop when the condition is false
Body of the loop
This will loop forever!
Keep looping until the condition is true
Stop when the condition is true
This will loop forever!
12
Do Loop - Conditions
Do While or Until ltconditiongt 'Body of the
loop Loop
Displays AAA1 AAA2 AAA3 AAA4 AAA5
Changes KeepLooping to False which results the in
the loop ending
Condition is defined in 1 line
13
Do Loop Order of Execution
Body of Loop and Condition are exactly the same
Do While or Until ltconditiongt 'Body of the
loop Loop
Do 'Body of the loop Loop While or Until
ltconditiongt
  • Condition is evaluated before the body of the
    loop is executed
  • The body of the loop is not guaranteed to be
    executed
  • Condition is evaluated after the body of the loop
    is executed
  • The body of the loop is guaranteed to be executed
    at least 1 time

Doesnt show anything!
Shows AAA!
14
Demonstration
  • Do Loops
Write a Comment
User Comments (0)
About PowerShow.com