VB.Net

VB.Net Loops

VB.Net Loops

There may be scenarios when you need to execute a block of code numerous times. In general, statements are executed in the following order: the first statement in a function is executed first, then the second, and so on.

Programming languages have a variety of control structures that enable more complex execution routes.

A loop statement allows us to run a statement or group of statements many times. The general form of a loop statement in most programming languages is as follows:


Loop Architecture


Following are the types of looping statements available in VB.Net:

Loop Type

Description

Do Loop

It loops through the enclosing block of statements as long as a Boolean condition is True or until the condition becomes True. It might be ended at any time by using the Exit Do statement.

For...Next

It repeats a set number of statements, and a loop index counts the number of loop iterations while the loop executes.

For Each...Next

It iteratively repeats a set of statements for each element in a collection. This loop is used to access and manipulate all elements in an array or a VB.Net collection.

While... End While

As long as a specified condition is True, it will execute a series of statements.

With... End With

It's not quite a looping construct. It runs a series of statements that all relate to the same object or structure.

Nested loops

You can nest one or more loops within another While, For, or Do loop.

Loop Control Statements

Control statements in a loop alter the execution sequence. When execution exits a scope, all automated objects produced inside that scope are deleted.

Following control statements are available in VB.Net: 

Control Statement

Description

Exit statement

The loop or select case statement is terminated, and execution is transferred to the statement immediately following the loop or select case.

Continue statement

This instructs the loop to skip the rest of its body and immediately retest its condition before reiterating.

GoTo statement

Control is passed to the labeled statement. However, using the GoTo statement in your program is not recommended.

Top course recommendations for you

    VLOOKUP in Excel
    1 hrs
    Beginner
    45.2K+ Learners
    4.56  (3499)
    Blockchain Process
    1 hrs
    Beginner
    7K+ Learners
    4.59  (663)
    GO Programming Language
    1 hrs
    Beginner
    6.8K+ Learners
    4.49  (532)
    Conditional Formatting in Excel
    1 hrs
    Beginner
    17.6K+ Learners
    4.52  (1682)
    Fibonacci Series in Python
    1 hrs
    Beginner
    2.2K+ Learners
    4.61  (92)
    Design App
    1 hrs
    Beginner
    18.3K+ Learners
    4.45  (1208)
    Pivot Tables in Excel
    1 hrs
    Beginner
    22.1K+ Learners
    4.57  (2734)
    Divide and Conquer Algorithms
    1 hrs
    Beginner
    2.3K+ Learners
    4.59  (376)
    QR Code Generator in Python
    1 hrs
    Beginner
    4.8K+ Learners
    4.36  (212)
    Backtracking Algorithm
    1 hrs
    Beginner
    2.7K+ Learners
    4.55  (286)