TISHITU Visual Basic 6.0 For loop with Step Explain Part-2 @TarunShrivastava
TISHITU Visual Basic 6.0 For loop with Step Explain Part-2  @TarunShrivastava
Uploaded August 2013 | Updated September 2026, 2 weeks ago
Tishitu explains


For-Next Loops with step

In situations where you merely want to run the loop a predefined number of times, it can become quite tiresome to have to create and manage a counter for each loop, which is why we also have something called a For-Next Loop. This kind of loop allows you to specify a counter, to tell it to count from one number to another each time through the loop, and to exit once the counter has reached its upper limit. The syntax is as follow:




Dim I As Integer
   
For I = (Integer) To (Integer)
   (Code to execute)
Next I 
We used the variable name "I" above, as it is the most common name used for For-Loops; however, you can use any variable name you want, so long as the variable is of the type Integer. Now, let's improve our Fibonacci program even further:




Public Sub Main()
   Dim X As Integer
   Dim Y As Integer
   Dim cnt As Integer 'Our counter.
   
   For cnt = 1 To 8
      Debug.Print X
      X = Y + X
      Y = X - Y
   Loop
End Sub
 




In the example above, we first dimensioned cnt as an Integer, and then, in the declaration of the For-Next loop, set its value to 1. Each time through the loop, the value of cnt was incremented by 1 until it reached 8, at which point the loop was executed.




Exit For 




As with Do Loops, there is a statement that can be used to exit a For-Next loop, and it is called Exit For. Simply invoke this statement anywhere within a For-Next loop and the current loop will be exited.
Step 




By default, the variable used in the declaration of the For-Next loop is incremented by 1 each time through the loop; however, if you want to increment this value by a different amount each time through the loop, you can simply append Step (Integer) to the end of the For-Next loop declaration. If, for instance, we wanted to print out every even number counting backward from 20 to 0, we could do this using the following code:




Dim I As Integer
   
For I = 20 To 0 Step -2
   Debug.Print I
Next I
So there you have it now you can use loops all over your Visaul Basic 6 programs. These are one of the most useful tools you have. You might want to bookmark this tutorial so that later you can reference back to this great VB6 loop examples. If you have any questions or comments please post them below.

TISHITU
ISO: 9001-2008
RESEARCH AND CONSULTANCY CELL OF INDUSTRIAL APPLICATION
A Joint Accreditation System of Australia and New Zealand
Copyright © All Rights Reserved tishitu.org Reg No.08122629691/SSI
Accreditation No. M3111204IN

-~-~~-~~~-~~-~-
Please watch: "Lifi Communication by Arduino UNO Download Project"
youtube.com/watch?v=c4gC8dbaiZg
-~-~~-~~~-~~-~-
TISHITU Visual Basic 6.0 For loop with Step Explain Part-2LiFi communication Audio TishituTISHITU Flash Back Test and Fun with HHO Diwali Nights DAY-4 Happy Safe and Green DiwaliOverhead Tank Controller with code and simulationTISHITU #4 Tutorial Visual Basic 6.0 how to use Operators + , - , / , *,...HHO BOMB. Happy Diwali 2019TISHITU Android First Application Hello World with Android Emulator and configure System settings
Tishitu |

TISHITU Visual Basic 6.0 For loop with Step Explain Part-2

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER