Looping Structures in Visual Basic 2010

Looping Structures in Visual Basic 2010

Looping structures are very popular in any programming language. They are used to repeat a line(s) of code until a certain condition is not met. Moreover; they are called to iterative or repetitive structures. You can also use looping structures to repeat a certain line(s) of code for a specific number of times.

The below structure is used to give you a better conception of the looping structures.

Picture 1

Basically there are two main types of looping structures in Visual Basic 2010.

  1. Condition Controlled:

In this loop, body of the loop is repetitively executed until a certain condition become true.

Examples of it can be Do-loop, and While-Wend loop

  1. Counter Controlled:

In this loop, body of the loop is repeated for a specific number of times. Here an integer is used as a counter whole value changes during execution. When counter attains a predetermined value, the repetition is terminated.

Visual Basic supports different types of looping structures. We will discuss the most common types of them below;

Do While – Loop

Syntax:

The syntax first evaluates the condition. If the condition is true then executes the statement(s) and checks the condition again. If the condition is false then the program transfers the control out of the loop.

Code example: (Sum of the first 10 natural numbers.)

Output:

Output1 Looping

See Also:   String Manipulation Functions in Visual Basic 2010

Do – Loop While

Syntax:

In this looping structure; the statement(s) are executed first. Then the program tests the condition. If the condition is true, the program executes the statements once again. This process is repeated until the condition becomes false. Once the condition becomes false, the control is taken out of the loop.

Code example: (Sum of the first 10 natural numbers.)

Output:

Output1 Looping

See Also:   Become A Computer Programmer

For – Next Loop

Syntax:

There are situations where you want a line or a number of lines of code to be executed for a specific number of times. Here the number of repetitions do not depend on any condition. But it is simply a specific number of repetition.

In the syntax; counter is a Numeric variable. Start-value is assigned to counter and it increments by increment-value until it reaches the end-value. Once the counter reaches the end-value, the control is terminated and taken out of the loop. By default the increment-value is 1, but you can change it.

Code example: (Sum of the first 10 natural numbers.)

Output:

Output1 Looping

Visual Basic 2010; also supports While-Wend Loop, Do Until-Loop and Do-Loop Until!
We will discuss the remaining types of looping structures in the upcoming articles.

Previous String Manipulation Functions in Visual Basic 2010
Next The most interesting Google Tricks and Games you must try once!

About author

Bahonar Javid
Bahonar Javid 43 posts

<p>* Computer Application Student<br /> * Co-Founder of www.codejow.com<br /> * Love to code and Learn new languages<br /> * Enjoy playing football</p>

View all posts by this author →

You might also like