Download presentation
Presentation is loading. Please wait.
1
Introduction to Repetition
Loops Introduction to Repetition
2
Problem What do we do when we want to do something over and over again? E.g. Add the numbers 1 – 10 to a list box… lstTest.Items.Add(1) lstTest.Items.Add(2) lstTest.Items.Add(3) lstTest.Items.Add(4) lstTest.Items.Add(5) lstTest.Items.Add(6) lstTest.Items.Add(7) lstTest.Items.Add(8) lstTest.Items.Add(9) lstTest.Items.Add(10)
3
Solution Use a loop (repetition)
Start of loop (set rules for number of repeats) Stuff we want to repeat End of Loop
4
Types of Loops Type 1 For-Next Loop
This loop repeats a section of code a specified number of times. So if we wanted to do a task 1000 times, this kind of loop is ideal. Type 2 While Loop (Pre processing) This loop repeats a section of code until a certain condition is met, that is the condition may be met on the first attempt or on the thousandth, we simply don’t know! Type 3 Do Loop (Post processing) Always runs the code once then continues until a certain condition is met.
5
To make loops work… For any loop to work we need to know three things…
When the loop will start When the loop will end Where the loop is up to at a given point
6
Do While Loop Structure
while (Condition) { //code to repeat goes here }
7
Things to Watch with Do While Loops!
What is wrong with the following loops?
8
Indexes Counters and Accumulators
9
Indexes Counters and Accumulators
Answers the question “How Many?” How many loan payments?
10
Indexes Counters and Accumulators
Answer the question “How much?”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.