How to Program a Loop in VB
There are many different types of Loop within VB (Visual Basic) such as a ‘For Loop’, ‘Do Loop’ etc. The Loop we’ll take a loop at is a For Loop. A For Loop is used mainly when the Programmer wants to define how many times the loop will run.
STEP BY STEP
STEP
STEP 1
Firstly we need to create a file. So open up Notepad or any other text editing software. Then Import a Class Library, Create a Public Class and then create a Sub (Method). Like So:
-
Imports System.Console
-
Public Class Loop
-
Shared Sub Main()
-
Remember to close the Class and the Sub at the end of the file. ‘
-
STEP 2
Now we need to create the Loop. Which is a For Loop. Mainly and For loop is used when the programmer wants to define how many times the Loop loops for.
- For X = 1 to 10
This will create a loop that loop 10 times. I’ve called this Loop X.
STEP 3
Below that line we need to create something that will loop. Here, a WriteLine() will be used to visually see how the loop works.
- WriteLine(“Looping…”)
As you can see this line will print ‘Looping…’ to the command window. But with the help of the For Loop it will repeat 10 times. ‘
STEP 4
Closing the Loop is pretty and simple. We just need to tell the compiler to replay the loop until it has looped 10 times.
-
Next X
-
Remember: If you call your For Loop ‘Loopy’ then the Next X should be changed to Next Loopy.
Category: How-To, Programming Tutor










