For Next Looping My first Looping Structure
For…Next FOR counter_variable = initial_value TO end_value STEP increment Statement-1 Statement-2 ……. Statement-n NEXT counter_variable
For…Next
For…Next You use For...Next statements to execute a block of statements a specific number of times. For loops use a counter variable whose value is increased or decreased with each repetition of the loop.
For…Next Part of the structure Index or counter Starting value Ending value Next (increments counter) Iteration (one execution of the statement inside the loop)
Action of For...Next structure Deterministic loop For i = start To end statement1 statement2... I <=ending value i >ending value Next (i) For i = start To end statement1 statement2... Next (i) statement Increment counter Here
For Next Loops deterministic Repeats a statement OR a group of statements a specified number of times Dim intStoreNumber As Integer For intStoreNumber = 1 To 20 ' Check the order ' Fetch correct order ' Carry back Stales Next intStoreNumber
For…Next with Step Dim intStoreNumber As Integer For intStoreNumber = 1 To 19 Step 2 ' Check the order ' Fetch correct order ' Carry back empties Print intStoreNumber Next (increments counter by the step) How many times will this loop execute?
Simple loop Dim x as integer, _ var1 as string, _ var2 as string, _ var3 as string For x = 1 To 50 input #1, var1,var2,var3 Call PrintData(var1,var2) Next x
Step Increments You can increase or decrease the counter variable by a value other than 1 by using the Step keyword. Example Dim intCounter as integer, _ intTotal as integer For intCounter = 2 To 10 Step 2 intTotal = intTotal + intCounter Next intCounter Is intTotal a counter or accumulator?
Nesting: What is the Outcome Dim Outer as integer, Inner as integer For Outer = 1 To 8 Print Outer; For Inner = 1 To Outer Print "X "; Next Inner Print Next Outer
Outcome of the loop 1 X 2 XX 3 XXX 4 XXXX 5 XXXXX 6 XXXXXX 7 XXXXXXX 8 XXXXXXXX