Page 1 of 1

Loops This topic gives a brief on Loops in VB.NET Rate Topic: -----

#1 mr.coder  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 41
  • Joined: 31-January 10

Posted 05 February 2010 - 09:44 AM

What are Loops ?
Loops enable us to execute a series of expression multiple number of times within a program.

So lets start with the Loops used in VB.NET :

1. For Loop
The 'For' Loop is the most popular loop with a simple auto increment index. The 'For' Loop needs a loop index which count the number of loop iterations as the loop executes.

Syntax:

For <Index> = <Start> To <End> [Step <Step Value>]
'Statements
Next

The Index variable is set to start automatically when the Loop starts. The Index value is incremented or decremented by the specified (+/-)Stepvalue and continues until the condition to loop is met. If you do not write any step value, then by default, the increment is considered to be 1.

Example:


    Sub Main()
        For index As Byte = 1 To 10 Step 1
            Console.WriteLine("Loop Index - " & index)
        Next
    End Sub




2. For Each Loop
The Visual Basic collection objects support a special type of loop, the 'For Each' Loop, which can be used to execute a block of code for each element in the collection instead of executing the block a set number of times. The 'For Each' Loop is similar to simple 'For' Loop with the difference that, the 'For Each' Loop is used on Collections.

Syntax:

For Each <Index> In <Collection>
'Statements
Next

Example:


    Sub Main()
        Dim arr(2) As Integer
        arr(0) = 0
        arr(1) = 1
        arr(2) = 2

        For Each index As Byte In arr
            Console.WriteLine("Array Values - " & index)
        Next
        Console.ReadLine()
    End Sub




3. While Loop
The 'While' Loop keeps executing until the condition against which it tests remains true. Unlike 'For' Loop, the index in 'While' Loop needs to be incremented manually.

Syntax:

While <Condition>
'Statements
'Index Increment
End While

Example:


    Sub Main()
        Dim index As Byte = 1
        While (index < 5)
            Console.WriteLine("Index Value - " & index)
            index = index + 1
        End While
        Console.ReadLine()
    End Sub




4. Do Loop
The 'Do' Loop can be used to execute a fixed block of statements indefinite number of times. The 'Do' Loop keeps executing it's statements while or until the condition is True. Like 'While' Loop, the index in 'Do' Loop needs to be incremented manually. The 'Do' Loop can be used along with two keywords which can be stated as:

i. 'Do While' Loop

Syntax:

Do While <Condition>
'Statements
'Index Increment
Loop

Example:


    Sub Main()
        Dim index As Byte = 1
        Do While (index < 5)
            Console.WriteLine("Index Value - " & index)
            index = index + 1
        Loop
    End Sub




ii. 'Do Until' Loop

Syntax:

Do Until <Condition>
'Statements
'Index Increment
Loop

Example:


    Sub Main()
        Dim index As Byte = 1
        Do Until (index = 5)
            Console.WriteLine("Index Value - " & index)
            index = index + 1
        Loop
    End Sub




Is This A Good Question/Topic? 2
  • +

Replies To: Loops

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,410
  • Joined: 29-May 08

Posted 06 February 2010 - 01:59 PM

A nice simple Tutorial that explains the details of the various loop structures.
Was This Post Helpful? 1
  • +
  • -

#3 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Posted 13 February 2010 - 02:39 AM

You are missing some basics of the loops (or what I think are basics). Here are some thoughts how to improve the tutorial:

- Mention that you can't change the collection during for each loop.

- It might be a good idea to give an example of a Do loop which executes the code at least once like
Do 
' something 
Loop Until i = 10



- Explain Continue [loop keyword] and Exit [loop keyword] commands.
Was This Post Helpful? 0
  • +
  • -

#4 Stippadippa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 10-July 09

Posted 24 February 2010 - 11:08 AM

Would be good to add outputs of the codes as somebody don't know what will those loops do.

This post has been edited by Stippadippa: 24 February 2010 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 mohittalniya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-March 10

Posted 04 April 2010 - 02:24 AM

how to pause output window
Was This Post Helpful? 0
  • +
  • -

#6 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 635
  • Joined: 03-February 10

Posted 04 June 2010 - 11:06 AM

For Each doesn't just operate on collections -- anything that supports IEnumerable and IEnumerate.
Was This Post Helpful? 0
  • +
  • -

#7 MistGun  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 27-October 10

Posted 28 October 2010 - 03:03 PM

Nice tutorial bro! it would help new coders to understand the loop.
Thanks for the contribution!
Was This Post Helpful? 0
  • +
  • -

#8 KNG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-August 11

Posted 17 August 2011 - 08:13 AM

(index = index + 1)


why do you have to put this in your "DO WHILE LOOP" code. It is already stated there that it is a "LOOP" sir :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1