TISHITU Visual Basic 6.0 For loop Explain Part-1

Опубликовано: 01 Июнь 2026
на канале: Tishitu
15,672
57

Tishitu explains

For-Next Loops 

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.

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

~-~~-~~~-~~-~
Please watch: "Lifi Communication by Arduino UNO Download Project"
   • Lifi Communication by Arduino UNO Download...  
~-~~-~~~-~~-~