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
    46.9K+ Learners
    4.57  (3856)
    Blockchain Process
    1 hrs
    Beginner
    7.2K+ Learners
    4.59  (756)
    GO Programming Language
    1 hrs
    Beginner
    7.1K+ Learners
    4.5  (595)
    Conditional Formatting in Excel
    1 hrs
    Beginner
    18.3K+ Learners
    4.52  (1809)
    Fibonacci Series in Python
    1 hrs
    Beginner
    2.3K+ Learners
    4.63  (103)
    Design App
    1 hrs
    Beginner
    18.8K+ Learners
    4.44  (1311)
    Pivot Tables in Excel
    1 hrs
    Beginner
    23.3K+ Learners
    4.57  (3092)
    Divide and Conquer Algorithms
    1 hrs
    Beginner
    2.5K+ Learners
    4.59  (461)
    QR Code Generator in Python
    1 hrs
    Beginner
    4.9K+ Learners
    4.4  (229)
    Backtracking Algorithm
    1 hrs
    Beginner
    2.8K+ Learners
    4.57  (327)