Title: Loops
1Loops
- Tonga Institute of Higher Education
2Introduction
- Programs need to be able to execute tasks
repeatedly. - Use loops to repeat actions
- For Loops
- Do Loops
3Loops
- 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.
4For 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
5For 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
6For 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
7For 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
8For 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
9Demonstration
10Do 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
11Do 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!
12Do 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
13Do 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!
14Demonstration