Download presentation
Presentation is loading. Please wait.
Published byGeorge Powers Modified over 9 years ago
1
5b – For Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming
2
Loops – For Next 2 Another variation of Loops – use a counter to determine when to exit the loop –Need a counter variable. –Need a starting value for the counter variable. –Need an ending value for the counter variable so the loop can stop. –Need to specify how the counter is changed so it can reach the stopping condition. Counter is changed after the loop body is executed.
3
Loops – For Next 3 E.g. Output ten lines of **************: For intCounter = 1 To 10 Step 1 lstOutput.Items.Add(“**********”) Next Can achieve the same effect by using a Do While Loop: intCounter = 1 Do While intCounter <= 10 lstOutput.Items.Add(“**********”) intCounter = intCounter + 1 Loop
4
Loops – For Next 4 E.g. Add up all the integers from 1 to 100 ‘Notice that the counter variable is used in the computation For intCounter = 1 To 100 Step 1 intSum = intSum + intCounter Next Step 1 can be omitted if increment by 1. The above is the same as: For intCounter = 1 To 100 intSum = intSum + intCounter Next
5
Loops – For Next 5 Other examples: ‘Add up 0, 2, 4, 6, 8, … For intCounter = 0 To 15 Step 2 ‘ increase counter by 2 intSum = intSum + intCounter Next ‘Add up 0, 3, 6, 9, … For intCounter = 0 To 15 Step 3 ‘ increase counter by 3 intSum = intSum + intCounter Next ‘Output all the odd numbers smaller than 20 For intCounter = 1 To 20 Step 2 ‘ increase counter by 2 lstOutput.Items.Add(intCounter) Next
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.