Presentation is loading. Please wait.

Presentation is loading. Please wait.

For loops intCount = 1 inCount = intCount + 1 True intCount

Similar presentations


Presentation on theme: "For loops intCount = 1 inCount = intCount + 1 True intCount"— Presentation transcript:

1 For loops intCount = 1 inCount = intCount + 1 True intCount
For intCount = 1 To 5 Me.CreateGraphics.DrawEllipse(objPen, _ 0, intYCoord, 50, 50) intYCoord += 110 Next intCount intCount = 1 inCount = intCount + 1 Draw circle True intCount <= 5 False Draws 5 circles

2 For loops Numeric Anything that variable has a numeric value
For Index = InitialValue To TestValue _ Step Increment statements Next Index Optional If omitted, the increment is 1 Can just write Next But clearer to write Next Index

3 For loops For Index = InitialValue To TestValue _ Step Increment
statements Next Index index = InitialValue index = index + Increment statements True index <=,>= TestValue if Increment > 0 if Increment < 0 False

4 Counting in For loops We can print a line of 5 circles: OOOOO
How would you print? OOOOO use: repeat 5 times ‘Changing X coordinates Me.CreateGraphics.DrawEllipse (objPen, intXCoord, intYCoord, _ 50, 50) repeat 3 times ‘Changing Y coordinates Nested For loops

5 In VB inner loop: print one row of 5 circles outer loop: once a line
' Create a Pen object. Dim objPen As New Pen(Color.Red, 1) Dim intNumCols, intNumRows, intInnerCounter, _ intOuterCounter, intXCoord, intYCoord As Integer intNumCols = 5 intNumRows = 3 For intOuterCounter = 1 To intNumRows For intInnerCounter = 1 To intNumCols Me.CreateGraphics.DrawEllipse(objPen, _ intXCoord, intYCoord, 100, 100) intXCoord += 110 ‘update X coordinates Next intInnerCounter intXCoord = 0 ‘reset X coordinates intYCoord += 110 ‘update Y coords to move down Next intOuterCounter inner loop: print one row of 5 circles outer loop: once a line of 5 circles is printed, Increase Y coordinates All of this 3 times

6 Loop Pitfalls For intCount = 1 To 10 Step 2 intCount = 1 Next intCount
Get a never ending loop What happens? intFinal = 10 For intCount = 1 To intFinal intFinal = -5 Me.CreateGraphics.DrawEllipse(pen, _ 0, intYCoord, 50, 50) Next intCount VB doesn't take into account the modification for the loop counting The loop is executed 10 times What happens? In a loop, don't change the values of the variables used for the counting


Download ppt "For loops intCount = 1 inCount = intCount + 1 True intCount"

Similar presentations


Ads by Google