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
    44.8K+ Learners
    4.57  (3373)
    Blockchain Process
    1 hrs
    Beginner
    6.9K+ Learners
    4.56  (578)
    GO Programming Language
    1 hrs
    Beginner
    6.7K+ Learners
    4.48  (510)
    Conditional Formatting in Excel
    1 hrs
    Beginner
    17.4K+ Learners
    4.51  (1637)
    Fibonacci Series in Python
    1 hrs
    Beginner
    2.2K+ Learners
    4.6  (91)
    Design App
    1 hrs
    Beginner
    18.1K+ Learners
    4.44  (1180)
    Pivot Tables in Excel
    1 hrs
    Beginner
    21.4K+ Learners
    4.54  (2301)
    Divide and Conquer Algorithms
    1 hrs
    Beginner
    2.3K+ Learners
    4.57  (332)
    QR Code Generator in Python
    1 hrs
    Beginner
    4.7K+ Learners
    4.36  (210)
    Backtracking Algorithm
    1 hrs
    Beginner
    2.6K+ Learners
    4.49  (205)