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
    34K+ Learners
    4.6  (1342)
    Blockchain Process
    1 hrs
    Beginner
    5.8K+ Learners
    4.54  (345)
    GO Programming Language
    1 hrs
    Beginner
    4.9K+ Learners
    4.44  (317)
    Conditional Formatting in Excel
    1 hrs
    Beginner
    12.8K+ Learners
    4.57  (515)
    Fibonacci Series in Python
    1 hrs
    Beginner
    1.9K+ Learners
    4.65  (68)
    Design App
    1 hrs
    Beginner
    14.7K+ Learners
    4.47  (711)
    Pivot Tables in Excel
    1 hrs
    Beginner
    13.9K+ Learners
    4.6  (615)
    Divide and Conquer Algorithms
    1 hrs
    Beginner
    1.7K+ Learners
    4.6  (92)
    QR code in Python
    1 hrs
    Beginner
    4K+ Learners
    4.42  (154)
    Backtracking Algorithm
    1 hrs
    Beginner
    1.9K+ Learners
    4.49  (80)